split list into n chunks of size n python

How to Iterate Over List in Chunks

Often python developers need to iterate over lists in Python. They do so one item at a time. But sometimes you may need to work with multiple list items at once. In such cases, you will need to iterate over list in chunks. In this article, we will learn how to iterate over list in chunks. There are several ways to do this. We will look at a couple of them. They are useful in splitting or partitioning lists.

How to Iterate Over List in Chunks

Let us say you have the following list in python.

letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

Here is a simple function that allows you to iterate over any iterable, not just lists, in chunks.

def chunker(seq, size):
    return (seq[pos:pos + size] for pos in range(0, len(seq), size))

In this function, we basically input iterable seq and size of chunk. We also use an index pos to get the starting point of chunk. We use a range() function to create step wise list of values of pos index, starting from zero, with a step size equal to chunk size. We use a for loop to iterate through this list of values and return iterator for chunks of the iterable, from pos to pos+size positions.

It works with any iterable not just lists as shown below.

letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

for group in chunker(letters, 3):
# ['a', 'b', 'c']
# ['d', 'e', 'f']
# ['g', 'h']

If you are comfortable using itertools & zip_longest function, then you can do the same thing using the following function.

from itertools import zip_longest

def grouper(iterable, n, fillvalue=None):
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)

In the above function, we use zip_longest function from itertools module. It accepts an argument of iterable, chunk size and fillvalue (None). It creates and inputs an iterable n times that of input iterable, to zip_longest which extracts separate chunks and returns them.

if you are using python<3, then you need to import izip_tools from itertools, instead of using zip_tools.

In this article, we have learnt a couple of simple ways to iterate over list in chunks. As mentioned before, they also work on other iterables such as a string text, that you may need to split into chunks.

Also read:

How to Create Python Dictionary from String
How to Strip HTML from Text in JavaScript
How to Preload Images With jQuery
How to Make Python Dictionary from Two Lists
How to Check if JavaScript Object is Undefined

Leave a Reply

Your email address will not be published.