Home  >  Article  >  Backend Development  >  How to split a list into chunks of the same size in Python

How to split a list into chunks of the same size in Python

silencement
silencementOriginal
2019-05-23 14:02:5210446browse

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.

How to split a list into chunks of the same size in Python

In Python, a generator can be used to divide a list into small lists of equal sizes.

Use generator:

How to split a list into chunks of the same size in Python

##Test

How to split a list into chunks of the same size in Python

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn