def chunks(l, n):
n = max(1, n)
return (l[i:i+n] for i in range(0, len(l), n))
# Split a Python List into Chunks using For Loops
sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
chunked_list = list()
chunk_size = 2
for i in range(0, len(sample_list), chunk_size):
chunked_list.append(sample_list[i:i+chunk_size])
print(chunked_list)
# Split a Python List into Chunks using itertools
from itertools import zip_longest
sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
chunk_size=2
result=list(zip_longest(*[iter(sample_list)]*chunk_size, fillvalue=''))
print(result)
chunked_list = [list(item) for item in list(zip_longest(*[iter(sample_list)]*chunk_size, fillvalue=''))]
print(chunked_list)