Write a Python Program to Split a List Into Evenly Sized Chunks

In Python, it’s often useful to split a list into evenly sized chunks.

This can be useful for processing data in parallel, or for other tasks where you need to work with smaller subsets of a larger dataset.

Fortunately, Python provides a simple way to split a list into chunks using the built-in chunk method from the itertools module.

Here’s an example of how you can use this method to split a list into chunks of a specific size:

from itertools import islice

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

In this example, we’re defining a new function called chunks that takes two arguments: lst, which is the list that we want to split into chunks, and chunk_size, which is the size of each chunk.

The range function is used to generate a sequence of indexes that we’ll use to slice the list into smaller chunks.

We start at 0, and increment by chunk_size on each iteration, until we reach the end of the list.

The yield keyword is used to return each chunk as a generator object, rather than returning a complete list of chunks all at once.

This can be more efficient in terms of memory usage, especially for very large lists.

To use this function, you simply call it with your list and the desired chunk size, like so:

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
chunk_size = 3
for chunk in chunks(my_list, chunk_size):
    print(chunk)

This will output the following:

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10]

As you can see, the list has been split into four chunks of size 3, with the last chunk containing only a single item.


In conclusion, splitting a list into evenly sized chunks is a common task in Python, and it can be easily accomplished using the itertools module.

By using the yield keyword to generate a generator object for each chunk, we can avoid the memory overhead of generating a complete list of chunks all at once, making this method suitable for working with very large lists.