itertools.batched consumes a stream lazily; strict mode fails when its last tuple is short.
Make this comfortable
Python batched strict: reject an incomplete import group
Operation contract
A settlement import expects groups of three IDs. The first input has six IDs and produces two complete groups. A second input has seven; two groups can be yielded before iteration raises ValueError on the final lone ID. Validate the entire stream before committing irreversible group effects.
Failure boundary
Strict batching is not an atomic transaction. Previously yielded groups may already have been written when the final error appears. If all-or-nothing import is required, stage data in a transaction or temporary store first. The iterator also has no built-in total input limit.
Working program
from itertools import batched
accepted_ids = (f"receipt-{number}" for number in range(47, 53))
groups = list(batched(accepted_ids, 3, strict=True))
print("groups", len(groups))
print("first", groups[0])
incomplete_ids = (f"receipt-{number}" for number in range(47, 54))
complete_before_error = 0
try:
for settlement_group in batched(incomplete_ids, 3, strict=True):
complete_before_error += 1
except ValueError:
print("complete_before_error", complete_before_error)Output
groups 2
first ('receipt-47', 'receipt-48', 'receipt-49')
complete_before_error 2Costs and limits
Iteration takes O(n) time and retains up to three items per current batch. Materializing all groups with list, as in the first check, takes O(n) additional space.
Common Mistakes
- A late strict error does not undo earlier side effects.
- Strict mode checks final tuple length, not business validity of each item.
- An infinite input never reaches a final-batch check.
Connected lessons
python
itertools-batched-strict
