Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Python Split list into chunks using itertools Method

# 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)
Source by itsmycode.com #
 
PREVIOUS NEXT
Tagged: #Python #Split #list #chunks #itertools #Method
ADD COMMENT
Topic
Name
3+7 =