Intermediate · Coding exercise
Consume an iterator in batches
generators · laziness · memory
Guest practice stays in this visit. Sign in to save your progress. Checks cover the stated cases; discussion rubrics are self-reviewed. These tasks do not mark course lessons complete.
BuildConsume an iterator in batches
Implement batches(values, size) as a generator yielding lists of up to size items. Yield a final partial batch. Raise ValueError for size <= 0 when consumption begins. Do not read values until requested. Tests materialize output through collect_batches. Define that helper to return list(batches(values, size)).