Skip to main content

chunks

Split an iterator into chunks with n elements each.

def chunks(
it: iterator,
n: int
) - > generator

Split an iterator into chunks with n elements each. Warning: it must be an actual iterator, if you pass this a concrete sequence will get you repeating elements.

Parameters​

NameTypeDescription
ititeratorThe source iterator to be partitioned; must be a true iterator (e.g., via iter()) to avoid repeating elements.
nintThe maximum number of elements to include in each yielded chunk.

Returns​

TypeDescription
generatorA generator yielding lists, where each list contains up to n elements from the original iterator.