Skip to content

How to split a List into equally sized chunks in Python

Learn different ways you can use to split a List into equally sized chunks in Python.


Learn different ways you can use to split a List into equally sized chunks in Python.

The following methods can be used to batch data from an iterable into lists or tuples of equal length n:

Implement your own generator

You can implement your own generator like this:

my_list = list(range(10))

def chunk(lst, n):
    for i in range(0, len(lst), n):
        yield lst[i:i + n]


chunks = list(chunk(my_list, 3))
# [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

Use a one-liner

If you really want a one-liner, you can also use a generator or list comprehension:

n = 3

# Generator comprehension
chunks = (my_list[i:i + n] for i in range(0, len(my_list), n))
print(list(chunks))
# [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

# List comprehension
chunks = [my_list[i:i + n] for i in range(0, len(my_list), n)]
print(chunks)
# [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

Use itertools.zip_longest

You can use itertools.ziplongest. Note that when using itertools, the single chunks are tuples, and in this case the last batch gets filled with the specified fillvalue if the batch is shorter than n:

from itertools import zip_longest

def chunk(lst, n):
    return zip_longest(*[iter(lst)]*n, fillvalue=None)

chunks = list(chunk(my_list, 3))
# [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, None, None)]

Use itertools.islice

You can use itertools.islice. Here, the last batch does not get filled in case it is shorter than n:

from itertools import islice

def chunk(lst, n):
    it = iter(lst)
    return iter(lambda: tuple(islice(it, n)), ())

chunks = list(chunk(my_list, 3))
# [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9,)]

Use itertools.batched (New in Python 3.12)

In Python 3.12, you can use the new itertools.batched method, which was implemented exactly for this purpose:

from itertools import batched

chunks = list(batched(my_list, 3))
# [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9,)]

Use more-itertools

If you can't use Python 3.12 and don't want to reinvent the wheel, you can use the excellent third-party module more-itertools. It provides more routines for operating on iterables, beyond itertools.

Install it first:

pip install more-itertools

Then you can use these methods:

import more_itertools as mit

list(mit.chunked(iterable, n))
# [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

list(mit.sliced(iterable, n))
# [range(0, 3), range(3, 6), range(6, 9), range(9, 10)]

list(mit.grouper(n, iterable))
# [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, None, None)]

list(mit.windowed(iterable, len(iterable)//n, step=n))
# [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, None, None)]

list(mit.chunked_even(iterable, n))
# [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

Resources

All the mentioned methods in this post of chunking a list were summarized from this Stack Overflow thread.


FREE VS Code / PyCharm Extensions I Use

鉁 Write cleaner code with Sourcery, instant refactoring suggestions: Link*


PySaaS: The Pure Python SaaS Starter Kit

馃殌 Build a software business faster with pure Python: Link*

* These are affiliate link. By clicking on it you will not have any additional costs. Instead, you will support my project. Thank you! 馃檹