Use it when every item must appear once in consecutive batches and the final batch may be shorter than the requested size.
Keep true
Before each yield, indexes below start have appeared exactly once; items[start:start+batch_size] contributes the next non-overlapping range, clipped only by sequence length.
Reuse it
Use half-open slices and a fixed stride to partition ordered data without overlap; let bounded slicing handle a short tail explicitly.
Read it this way: For items [0,1,2,3,4,5,6] and batch_size=3, the positive-size guard passes. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Pattern: Step through a sequence by batch size.
Simple idea: Slice from each start position to start + batch_size. Python stops the last
slice at the sequence end.
from collections.abc import Iterator, Sequencedef batches(items: Sequence, batch_size: int) -> Iterator[Sequence]: if batch_size <= 0: raise ValueError("batch_size must be positive") for start in range(0, len(items), batch_size): yield items[start : start + batch_size]
Cost:O(n) total iteration time and O(batchsize) output per step.