Python

chunked — Split Iterable into Fixed-Size Pieces

admin by @admin ADMIN
Jun 19, 2026
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Lazy chunker that works on any iterable, not just lists. Common building block for batch APIs — "send 50 rows per request" — without loading the source into memory.
Python
Raw
from itertools import islice
from typing import Iterable, Iterator

def chunked[T](items: Iterable[T], size: int) -> Iterator[list[T]]:
    if size < 1:
        raise ValueError("size must be >= 1")
    it = iter(items)
    while batch := list(islice(it, size)):
        yield batch

list(chunked(range(7), 3))
# [[0, 1, 2], [3, 4, 5], [6]]

# Bulk-insert in batches of 1,000 — never load the whole file:
import sqlite3
with sqlite3.connect("db.sqlite") as db, open("huge.txt") as f:
    for batch in chunked(f, 1000):
        db.executemany("INSERT INTO lines VALUES (?)", [(line.strip(),) for line in batch])
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.