Home > Article > Backend Development > How to split a list into chunks of the same size in Python
How to split a list into blocks of the same size in Python: 1. Use a generator; 2. Use a for loop, the syntax "[l[i:i n] for i in range(0, len(l ), n)]", l is the list to be divided, and n is the number of parts.
In Python, a generator can be used to divide a list into small lists of equal sizes.
Use generator:
##Test
for loop:
##[l[i: i n] for i in range(0, len(l), n)], l is the list to be divided, n is the number of parts to be divided into
Test:
The above is the detailed content of How to split a list into chunks of the same size in Python. For more information, please follow other related articles on the PHP Chinese website!