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
| Name | Type | Description |
|---|---|---|
| iterable | iterable | Input values, consumed as needed. |
| n | int | Positive maximum batch size. |
| strict | bool | Reject 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)))