python split list into chunks of size n

How to Split List into Evenly Sized Chunks in Python

Python lists are useful data structures that allow you to easily store data of different types in a compact manner. Sometimes you may need to split list into evenly sized chunks in Python. Here are the steps to split python lists into even chunks.


How to Split List into Evenly Sized Chunks in Python

Here are the steps to split a python list into chunks of size N. Let us say you have the following list.

>>> a=[1,2,3,4,5,6,7,8,9]


1. Using Generator

In this approach, we will define a function that takes in the original list and chunk size as input, and outputs individual chunks.

If you are using python >3, you can use the following function to create N sized chunks from your python list.

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

This function requires you to input original list and the chunk size n. It uses a generator that creates and yields each chunk while iterating through the original list.

If you are using python 2, then you need to use xrange() function instead of range() function in the above function.

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

Here is an example to use the above functions to split our list into chunks of 3 items each. Please note, since our chunks function returns a generator, we will have to convert into list first and use pprint function to display it.

>>> chunks(a,3)
<generator object chunks at 0x0000000003A5A828>

>>> import pprint
>>> pprint.pprint(list(chunks(a, 3)))
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

If your list cannot be divided equally in N sized chunks, then the above function will create a list with leftover items. Here is an example to split the above list into chunks of 4 items.

>>> pprint.pprint(list(chunks(a, 4)))
[[1, 2, 3, 4], [5, 6, 7, 8], [9]]


2. Using List Comprehensions

In this approach, we simply use list comprehensions to split a list into N-sized chunks.

Here is an example list comprehension in python 3+.

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

If you are using python 2, use the following list comprehension instead.

[lst[i:i + n] for i in xrange(0, len(lst), n)]

In the above command, lst is the name of your list, n is the chunk size.

Here is an example to split our list ‘a’ into chunks of 3 items each.

>>> [a[i:i + 3] for i in range(0, len(a), 3)]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

If the list comprehension is unable to divide list into equal chunks of n elements, it will split the list into as many sublists with n items, and create a new list with leftover items. Here is an example to split our list into chunks of 4 items.

>>> [a[i:i + 4] for i in range(0, len(a), 4)]
[[1, 2, 3, 4], [5, 6, 7, 8], [9]]

In this article, we have learnt how to split list into chunks of size N.

Also read:

How to Fix Temporary Failure in Name Resolution In Linux
How to Add Route in Linux
How to Change Root Password in CentOS, RHEL, Fedora Linux
How to Ping Specific Port in Linux
How to Count Number of Files in Directory in Linux

Leave a Reply

Your email address will not be published.