Break a List into Chunks of Size N in Python (original) (raw)

Given a list of elements and a number n, the task is to split the list into smaller sublists (chunks), where each sublist contains at most n elements. This helps in processing large data in smaller parts or batches.

**For Example:

a = [1, 2, 3, 4, 5, 6, 7, 8]
n = 3
Result: [[1, 2, 3], [4, 5, 6], [7, 8]]

Let’s explore different methods one by one.

Using List Comprehension

List comprehension is an efficient method for chunking a list into smaller sublists. This method creates a list of lists, where each inner list represents a chunk of the original list of a given size.

Python `

a = [1, 2, 3, 4, 5, 6, 7, 8] n = 3 res = [a[i:i + n] for i in range(0, len(a), n)] print(res)

`

Output

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

**Explanation:

For very large lists, itertools.islice can be a memory-efficient way to create chunks without loading the entire list into memory. It allows you to process the list incrementally.

Python `

from itertools import islice a = [1, 2, 3, 4, 5, 6, 7, 8]
n = 3

it = iter(a) res = [list(islice(it, n)) for _ in range((len(a) + n - 1) // n)] print(res)

`

Output

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

**Explanation:

The zip_longest() function from the itertools module can be used to break a list into evenly sized chunks by grouping elements together in tuples of size n.

Python `

from itertools import zip_longest a = [1, 2, 3, 4, 5, 6, 7, 8] n = 3

res = [list(filter(None, group)) for group in zip_longest(*[iter(a)]*n)] print(res)

`

Output

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

**Explanation:

Using Slicing

This is a straightforward approach using a for loop to iterate over the list and slice it manually into chunks of size n. It’s simple to understand and works well for smaller datasets.

Python `

a = [1, 2, 3, 4, 5, 6, 7, 8] n = 3
res = [] for i in range(0, len(a), n): # Slice list in steps of n res.append(a[i:i + n])

print(res)

`

Output

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

**Explanation: