← Go Back

How to Iterate Over an Object by Chunks

The for loop allows you to go through the elements of an iterable object one at a time:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for n in numbers:
print(n)

Using the batched() function, you can loop through two or more elements:

# even receives a tuple of two elements.
for pair in batched(numbers, 2):
print(pair)

This prints:

(1, 2)
(3, 4)
(5, 6)
(7, 8)
(9, 10)


Another example: If the number of elements of the iterated object is not multiple of the second argument, then in the last iteration a tuple with fewer elements is received:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
for pair in batched(numbers, 2):
print(pair)

Output:

(1, 2)
(3, 4)
(5, 6)
(7, 8)
(9, 10)
(11,)


To iterate by chunks of 3 items:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
for triplet in batched(numbers, 3):
print(triplet)

Output

(1, 2, 3)
(4, 5, 6)
(7, 8, 9)
(10, 11, 12)


Here's the batched() function implementation (borrowed from Python recipes):

from itertools import islice

def batched(iterable, n):
"Batch data into tuples of length n. The last batch may be shorter."
# batched('ABCDEFG', 3) --> ABC DEF G
if n < 1:
raise ValueError('n must be at least one')
it = iter(iterable)
while batch := tuple(islice(it, n)):
yield batch

Starting from Python 3.12, batched() is shipped within the itertools standard module:

from itertools import batched

iterators iterables loops lists tuples dictionaries


🐍 You might also find interesting: