PythonMastery
Python programs · Useful programs · intermediate

Process a list in batches in Python

Handle a long list a few items at a time, for example saving records in groups of 4, with slicing and with itertools.batched() from Python 3.12.

Some work is done better in groups than one item at a time: saving records to a database, or sending requests to a service that accepts at most 100 items per request. This program splits a list into batches of a fixed size and passes each batch to a function.

The program

python
def process_in_batches(items, size, process):
    if size < 1:
        raise ValueError("size must be at least 1")
    for start in range(0, len(items), size):
        process(items[start:start + size])


def save_batch(batch):
    print(f"Saving {len(batch)} records: {batch}")


records = list(range(1, 12))
process_in_batches(records, 4, save_batch)
output
Saving 4 records: [1, 2, 3, 4]
Saving 4 records: [5, 6, 7, 8]
Saving 3 records: [9, 10, 11]

How it works

Another way: itertools.batched

Python 3.12 added itertools.batched(). It gives tuples, and it works with any iterable, not only lists. That includes a file, which you can then process in batches of lines without reading it all into memory:

python
from itertools import batched

names = ["Ada", "Linus", "Grace", "Alan", "Hedy", "Margaret", "Guido"]
for number, batch in enumerate(batched(names, 3), start=1):
    print(f"Batch {number}: {batch}")
output
Batch 1: ('Ada', 'Linus', 'Grace')
Batch 2: ('Alan', 'Hedy', 'Margaret')
Batch 3: ('Guido',)

Learn the idea: Lists & Sequences, Functions & Scoping, Itertools: Composable Iterator Building Blocks