How to chunk (batch) an iterable in :

it = iter(iterable)
chunks = iter(lambda: list(islice(it, CHUNK_SIZE)), [])

It uses the little-known form of `iter()` with two arguments where it repeatedly call the first argument until it returns the sentinel value provided in the second.

This is incidentally what more_itertools.chunked does. But may be you're not allowed to add dependencies or something…

· · Web · 1 · 1 · 2

@isagalaev TIL about the two-argument form of ‘iter’

Sign in to participate in the conversation

Server run by the main developers of the project 🐘 It is not focused on any particular niche interest - everyone is welcome as long as you follow our code of conduct!