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

silencement
Release: 2020-09-18 17:54:30
Original
10425 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!