Iteration Classes

itertools.batched()

Collect at most n successive elements into tuples. n must be positive; strict=True raises ValueError when the final batch is short.

Formal Syntax & Structure

python syntaxpython
itertools.batched(iterable, n, *, strict=False)

Parameters & Attributes

NameTypeDescription
iterableiterableInput values, consumed as needed.
nintPositive maximum batch size.
strictboolReject an incomplete final batch when true.

Return Value

iterator

The iterator result described above.

Working Example

Example Snippetpython
import itertoolsprint(list(itertools.batched(range(5),2)))
Python Reference