How to split a Python list into evenly sized chunks

Iteration in Python is repeating a set of statements until a certain condition is met. This is usually done using a for loop or a while loop. There are several ways to split a Python list into evenly sized-chunks. Here are the 5 main methods:

Method 1: Using a Loop with List Slicing

Use for loop along with list slicing to iterate over chunks of a list.

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

        
if __name__ == "__main__":
        
  lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  chunk_size = 3
  for chunk in chunked_list(lst, chunk_size):
      print(chunk)

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

Method 2: Using List Comprehension with range()

You can also use a list comprehension with range() to create the chunks and then iterate over them. This is the one line code rather than using yield.

Python
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]
chunk_size = 3
chunks = [lst[i:i + chunk_size] for i in range(0, len(lst), chunk_size)]

for chunk in chunks:
    print(chunk)

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

Method 3: Using itertools.islice()

You can also use islice function, which is provided by the the itertools module. It is used to create an iterator that returns selected elements from the iterable.

Python
from itertools import islice

def chunked_list(lst, chunk_size):
    it = iter(lst)
    while True:
        chunk = list(islice(it, chunk_size))
        if not chunk:
            break
        yield chunk

if __name__ == "__main__":
  lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  chunk_size = 3
  for chunk in chunked_list(lst, chunk_size):
      print(chunk)

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

Method 4: Using NumPy (for large arrays/lists)

While, you will be dealing with large lists or need high performance, using numpy is the best idea.

Python
import numpy as np

def chunked_list(lst, chunk_size):
    return np.array_split(lst, np.ceil(len(lst) / chunk_size))

if __name__ == "__main__":
  lst = [1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18]
  chunk_size = 4
  chunks = chunked_list(lst, chunk_size)

  for chunk in chunks:
      print(chunk)

Output
[1 2 3 4]
[5 6 7 8]
[ 9 10 11 12]
[13 14 15]
[16 17 18]

Method 5: Using a Generator Function

This Generator method is similar to list comprehension but more memory-efficient as it yields chunks one at a time.

Python
def chunked_list(list_data,chunk_size):
  for i in range(0,len(list_data),chunk_size):
      yield list_data[i:i + chunk_size]
    
    
if __name__ == "__main__":  
  lst = [11,12,13,14,15,16,17,18,19,20,21,22]
  chunk_size = 3
  for chunk in chunked_list(lst, chunk_size):
      print(chunk)

Output
[11, 12, 13]
[14, 15, 16]
[17, 18, 19]
[20, 21, 22]




Contact Us