Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Python Split list into chunks using for loop

# 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)
Comment

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)
Comment

PREVIOUS NEXT
Code Example
Python :: python ceiling division 
Python :: pyqt set focus 
Python :: python insert sorted 
Python :: Appending rows to a DataFrame 
Python :: pandas read csv python 
Python :: structure ternaire python 
Python :: isolationforest estimators 
Python :: delete element from matrix python 
Python :: change column names pandas 
Python :: how to use coordinates in python 
Python :: include in flask 
Python :: python animation 
Python :: pandas dataframe caption 
Python :: python return to top of loop 
Python :: AI chatbot in Python - NAYCode.com 
Python :: reply_photo bot telegram python 
Python :: requests sessions 
Python :: python glob.glob recursive 
Python :: add a column with initial value to an existing dataframe 
Python :: save and load model during training pytorch 
Python :: how to use ternary operater in python 
Python :: create new dataframe from existing data frame python 
Python :: how to post data to foreign key in django rest framework 
Python :: sort lexo python 
Python :: python dataframe appendisnt showing 
Python :: walrus operator python 3.8 
Python :: heapsort python 
Python :: df.loc a list of index 
Python :: knuth morris pratt algorithm 
Python :: how to iterate set in python 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =