ML Foundations

Keep iterator batches stable when using Python islice

See how lazy slices share a source iterator and when to materialize one batch before taking the next.

Example results for Keep iterator batches stable when using Python islice. second consumed first: 0, 1, 2
Illustration: Original illustration by Tensor Almanac
Evidence and testing

Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.

A slice is not a reserved batch

itertools.islice produces an iterator over selected elements of its input. When two lazy slices are created from the same source iterator, constructing them does not reserve separate ranges. The slice consumed first draws from the source first, regardless of the order in which the slice objects were created. [1]

That distinction matters if batch iterators are saved and consumed later by different parts of a program. The source has one forward position, so the order of consumption determines which elements each slice receives. Python iterator exhaustion also means that a consumed iterator cannot simply be replayed. [2]

References: [1], [2]

Materialize each batch before advancing

Run python examples/python_islice_shared_batches.py. The first fixture creates two slices of three elements from one source. It consumes the second slice first, so that slice receives the first three values; the first slice then receives the next three. The printed output shows the actual order. [1]

The second fixture starts with a fresh iterator. It converts each islice to a list before creating the next one. The resulting lists hold the first and second groups respectively, and assertions verify their contents. Materialization stores the current batch in memory; it does not require storing the entire source.

This approach is useful when a consumer needs a stable batch that can be revisited. If an iterator is partially consumed or the source changes, reason about those effects separately rather than assuming slice construction made a snapshot.

Executed with Python 3.12.14; stdout and checksum are saved in private evidence.

"""Show that lazy slices share iterator position until consumed."""
from itertools import islice

source = iter(range(6))
first = islice(source, 3)
second = islice(source, 3)
print('second consumed first:', list(second))
print('first consumed second:', list(first))
source = iter(range(6))
batch_one = list(islice(source, 3))
batch_two = list(islice(source, 3))
print('materialized one:', batch_one)
print('materialized two:', batch_two)
assert batch_one == [0, 1, 2]
assert batch_two == [3, 4, 5]

Recorded output

second consumed first: [0, 1, 2]
first consumed second: [3, 4, 5]
materialized one: [0, 1, 2]
materialized two: [3, 4, 5]

References: [1], [2]

State the limits

The fixed fixture is single-threaded and uses a finite range. It does not test concurrent consumers, queue ownership, memory limits or behavior of a mutable data source. A production batching pipeline should define who owns and consumes each iterator.

For very large batches, the list size matters. Choose the batch size for available resources and retain a separate source if the application needs to make another pass. This example makes no throughput claim.

References: [1], [2]

Sources

  1. Python itertools.islice
  2. Python iterator types