DEV Community

Avnish
Avnish

Posted on

Break a list into chunks of size N in Python

1. Using yield for Generators

The yield keyword enables creating a generator that can produce chunks of the list lazily.

Example:

def chunk_with_yield(data, size):
    for start in range(0, len(data), size):
        yield data[start:start + size]

data_list = ['alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta', 'eta', 'theta']
chunk_size = 3
print(list(chunk_with_yield(data_list, chunk_size)))
Enter fullscreen mode Exit fullscreen mode

Output:

[['alpha', 'beta', 'gamma'], ['delta', 'epsilon', 'zeta'], ['eta', 'theta']]
Enter fullscreen mode Exit fullscreen mode

2. Using a Loop and Slicing

This approach iterates over the list in steps and slices it into chunks.

Example:

data_list = [10, 20, 30, 40, 50, 60, 70]
chunk_size = 2
for i in range(0, len(data_list), chunk_size):
    print(data_list[i:i + chunk_size])
Enter fullscreen mode Exit fullscreen mode

Output:

[10, 20]
[30, 40]
[50, 60]
[70]
Enter fullscreen mode Exit fullscreen mode

3. Using List Comprehension

This method compresses the logic into a single-line statement.

Example:

data_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
chunk_size = 4
chunks = [data_list[i:i + chunk_size] for i in range(0, len(data_list), chunk_size)]
print(chunks)
Enter fullscreen mode Exit fullscreen mode

Output:

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10]]
Enter fullscreen mode Exit fullscreen mode

4. Using NumPy's array_split

Leverage NumPy for efficient chunking of numeric arrays or ranges.

Example:

import numpy as np

numbers = list(range(15))  # Creating a list from 0 to 14
chunked_array = np.array_split(numbers, 5)
print([list(arr) for arr in chunked_array])  # Convert back to list
Enter fullscreen mode Exit fullscreen mode

Output:

[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], [12, 13, 14]]
Enter fullscreen mode Exit fullscreen mode

5. Using itertools.islice

The islice function can split the iterable into fixed-size chunks.

Example:

from itertools import islice

def chunk_with_itertools(data, size):
    data_iter = iter(data)
    while chunk := list(islice(data_iter, size)):
        yield chunk

letters = "abcdefghijk"
chunk_size = 4
print(list(chunk_with_itertools(letters, chunk_size)))
Enter fullscreen mode Exit fullscreen mode

Output:

[['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i', 'j', 'k']]
Enter fullscreen mode Exit fullscreen mode

6. Using deque from collections

Deque supports fast popping, enabling an efficient way to chunk lists.

Example:

from collections import deque

def deque_chunker(data, size):
    dq = deque(data)
    while dq:
        yield [dq.popleft() for _ in range(min(size, len(dq)))]

words = ['red', 'blue', 'green', 'yellow', 'purple', 'orange', 'pink']
chunk_size = 3
print(list(deque_chunker(words, chunk_size)))
Enter fullscreen mode Exit fullscreen mode

Output:

[['red', 'blue', 'green'], ['yellow', 'purple', 'orange'], ['pink']]
Enter fullscreen mode Exit fullscreen mode

7. Partial Assignment with Reassignment

This technique splits the list by reassigning the leftover elements.

Example:

data_list = [100, 200, 300, 400, 500, 600, 700]
chunk_size = 3
while data_list:
    chunk, data_list = data_list[:chunk_size], data_list[chunk_size:]
    print(chunk)
Enter fullscreen mode Exit fullscreen mode

Output:

[100, 200, 300]
[400, 500, 600]
[700]
Enter fullscreen mode Exit fullscreen mode

These unique methods provide flexible ways to break a list into chunks, catering to various scenarios like lazy evaluation (using yield), performance optimization (numpy), or compact syntax (list comprehensions).

Top comments (0)