Break a list into chunks of size N in Python

In this article, we will learn how to break a list into chunks of any given size N in Python, with the help of a few simple examples.

A list is a collection of objects such as integers, floats, strings etc. Lists are mutable data structures, i.e the contents of the list can be changed without changing it’s identity. Breaking large lists into smaller chunks is a frequently used operation to make a program more efficient.

Using yield keyword

# Create a list
list1 = ['hi','hey','sup','good','bad','great','nice','bye','see you'] 

Here, we have declared a list which we want to break into chunks of size, let’s say 3. To do that, we will use the yield keyword.

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

# Size of each chunk
n = 3

x = list(divide_chunks(list1, n)) 
print (x)

So, the most crucial differentiating factor of the yield keyword is it’s ability to return to the position it left at during the previous iteration in a loop. In case of the above example, once the list is divided into a sub-list of 3 objects, the yield keyword enables the function to come back and resume from the 4th position.


[['hi', 'hey', 'sup'], ['good', 'bad', 'great'], ['nice', 'bye', 'see you']]

Using list comprehension

Another way to divide a list into smaller chunks is to use the list comprehension method. This is a straight forward way to do the task in one line of code.

# Using list comprehension 
x = print([list1[i: i+10] for i in range(0, len(list1), 10)]) 
print (x)


[['hi', 'hey', 'sup', 'good', 'bad', 'great', 'nice', 'bye', 'see you']]

Using this method for dividing large lists can be more complex. So, it is advisable to use the yield keyword method for such cases.

Also read: Sort list of list with custom compare function in Python

Leave a Reply

Your email address will not be published. Required fields are marked *