Python --How do you split a list into evenly sized chunks in Python?

Ref:

I found this very useful snippet in stackoverflow to split chunks of data. This code could be used to dispatch works to multiprocess.

def chunks(l, n):
    """ Yield successive n-sized chunks from l.
    """
    for i in xrange(0, len(l), n):
        yield l[i:i+n]

Here is how to use it:

import pprint
pprint.pprint(list(chunks(range(10, 75), 10)))
[[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
 [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
 [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
 [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
 [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
 [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
 [70, 71, 72, 73, 74]]

Recommended Posts

Python --How do you split a list into evenly sized chunks in Python?
Split iterator into chunks in python
How to clear tuples in a list (Python)
Until you insert data into a spreadsheet in Python
Why do you add a main ()-if statement in Python?
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
[Python] How to do PCA in Python
Do you need a Python re.compile?
How to make a string into an array or an array into a string in Python
What to do if you get a minus zero in Python
How to delete multiple specified positions (indexes) in a Python list
Do a non-recursive Euler Tour in Python
How to do R chartr () in Python
[Python] How to convert a 2D list to a 1D list
Display a list of alphabets in Python 3
How to get a stacktrace in python
How many types of Python do you have in Windows 10? I had 5 types.
How to format a list of dictionaries (or instances) well in Python
What happens if you do "import A, B as C" in Python?
How to pass the execution result of a shell command in a list in Python
How to embed a variable in a python string
How to create a JSON file in Python
Make a copy of the list in Python
View drug reviews using a list in Python
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
Sort list elements in a specified order in Python
What do you like about how to convert an array (list) to a string?
[Python] Manipulating elements in a list (array) [Sort]
How to get a list of files in the same directory with python
How to remove duplicate elements in Python3 list
Consideration when you can do a good job in 10 years with Python3 and Scala3.
How to identify the element with the smallest number of characters in a Python list?
You can do it in 3 minutes! How to make a moving QR code (GIF)!
How to check in Python if one of the elements of a list is in another list
How to find the first element that matches your criteria in a Python list
How to convert / restore a string with [] in python
How to do hash calculation with salt in Python
[Python] How to expand variables in a character string
How to write a list / dictionary type of Python3
% And str.format () in Python. Which one do you use?
Things to note when initializing a list in Python
BigQuery-If you get a Reason: responseTooLarge error in Python
If you encounter a "Unicode Decode Error" in Python
[Python] How to sort dict in list and instance in list
Group by consecutive elements of a list in Python
How to execute a command using subprocess in Python
[Python] How to output the list values in order
[Python] How do you use lambda expressions? ?? [Scribbles] [Continued-1]
How much do you know the basics of Python?
Sorted list in Python
Filter List in Python
List find in Python
[Python] How to delete rows and columns in a table (list of drop method options)
How to pass the execution result of a shell command in a list in Python (non-blocking version)
[Python version] Why can't you do object-oriented development in Java?
[Python] How to make a list of character strings character by character
How to slice a block multiple array from a multiple array in Python
A function that divides iterable into N pieces in Python
How to shuffle a part of a Python list (at random.shuffle)