Break a list into chunks of size N in Python



The objective is to break a list into chunks of a specific size (n), that is, splitting a list into sublists where each sublist contains n elements.

For example, if the list is [12, 33, 11, 56, 44, 89, 23, 34, 12] and the chunk size is 3. Then the output has to be - [[12, 33, 11],[56, 44, 89],[23, 34, 12]].

To break a list into chunks of size n in Python, we use list comprehensions, Numpy library, slicing and others. Let's discuss all the approaches in detail with examples -

Using List Comprehension

List comprehension provides a shorter syntax to create a new list based on the values of the existing list. This will create sublists of size n by iterating over the list in steps of n and breaking the list into chunks.

Example

In the example program below, we will use list comprehension to break a list into chunks of size n in Python -

a = [12, 33, 11, 56, 44, 89, 23, 34, 12]
n = 3 
print("Given list - ", a)

#Splits the list a into sublists of length n
output = [a[i:i + n] for i in range(0, len(a), n)]
print("List of chunks - ",output)

The output returned by the above code is as follows -

Given list -  [12, 33, 11, 56, 44, 89, 23, 34, 12]
List of Chunks -  [[12, 33, 11], [56, 44, 89], [23, 34, 12]]

Using the Yield Keyword

The yield keyword is used to create generators, which are iterators that allow values to be produced one at a time. This keyword creates a generator that returns chunks of the list of size n, one at a time.

Example

In the example program below, we will use the yield keyword to break a list into chunks of size n -

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

a = [12, 33, 11, 56, 44, 89, 23, 34, 12]
n = 3
print("Given list - ", a)
output = list(chunk_list(a, n))
print("List of Chunks - ", output)

The output returned by the above code is as follows -

Given list -  [12, 33, 11, 56, 44, 89, 23, 34, 12]
List of Chunks -  [[12, 33, 11], [56, 44, 89], [23, 34, 12]]

Using the Slice operator

The Slice operator in Python is used to extract a portion or subset of a sequence, like a list or a string. List slicing, along with a for loop, creates chunks of size n, efficiently splitting a list into smaller sublists.

Example

In the example program below, we will use the slicing technique to break a list into chunks of size n in Python -

a = [12, 33, 11, 56, 44, 89, 23, 34, 12]
n = 3  
output = [] 
print("Given list - ", a)

for i in range(0, len(a), n):  # Slice list in steps of n
    output.append(a[i:i + n])

print("List of Chunks - ", output)

The output returned by the above code is as follows -

Given list -  [12, 33, 11, 56, 44, 89, 23, 34, 12]
List of Chunks -  [[12, 33, 11], [56, 44, 89], [23, 34, 12]]

Using itertools.islice() Function

The itertools module in Python provides various functions that work on iterators to produce complex iterators. To break a list into chunks of size n, we use the islice() function in the itertools module.

The itertools.islice() function creates an iterator that returns selected elements from an iterable, similar to sequence slicing. The syntax of this function is -

itertools.islice(iterable, start, stop, step)

It accepts the following arguments as input -

  • Iterable: Data types that consist of elements in a sequence, like lists, tuples, or strings
  • start: Specifies the index to start slicing.
  • stop: Specifies the index to stop.
  • step: Specifies the increment between elements.

Example

In the example below, we will use the itertools.islice() function -

from itertools import islice

a = [12, 33, 11, 56, 44, 89, 23, 34, 12]  
n = 3  
print("Given list - ", a)

#Chunk the iterable 'a' into sublists of length 'n'.
output = [list(islice(iter(a), n)) for _ in range((len(a) + n - 1) // n)]
print("List of Chunks - ", output)

The output returned by the above code is as follows -

Given list -  [12, 33, 11, 56, 44, 89, 23, 34, 12]
List of Chunks -  [[12, 33, 11], [12, 33, 11], [12, 33, 11]]

Using NumPy array_split() Function

Numpy is an open-source Python library that provides a wide range of mathematical functions for array operations.

To break a list into chunks of size n, we use the array_split() function in the Numpy library, which accepts an array as an input parameter, splits it into multiple sub-arrays of approximately equal size along a specified axis.

Example

In the example below, we will use the array_split() function in Numpy to break a list into chunks of size 3 -

import numpy as np

a = [12, 33, 11, 56, 44, 89, 23, 34, 12] 
n = 3
print("Given list - ", a)

output = [list(output) for output in np.array_split(a, n)]
print("List of Chunks -", output)

The output returned by the above code is as follows -

Given list -  [12, 33, 11, 56, 44, 89, 23, 34, 12]
List of Chunks -  [[12, 33, 11], [12, 33, 11], [12, 33, 11]]
Updated on: 2025-06-09T14:25:31+05:30

552 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements