
A Python import has rows when you inspect it, but the processing loop sees fewer rows or none. Check whether both steps share the same iterator. Turning a generator into a list for logging, or calling any() to check it, can advance the stream before the real work starts. The data did not necessarily disappear at its source.
A boolean check can consume several items
rows = (value for value in [0, 2, 3])
print(any(rows)) # True
print(list(rows)) # [3]
print(list(rows)) # []
any() asks for values until it finds a truthy one. Here it consumes zero, then two, before returning True. The next consumer starts with three. If every value is falsey, any() consumes the finite iterator completely and returns False. It is not an empty-stream test: a nonempty stream of zeros also produces False.
Likewise, print(list(rows)) is not a harmless preview. Building that list consumes all remaining items of a finite iterator. A later loop over the same exhausted iterator gets nothing. An infinite stream makes whole-stream materialization an even worse choice because it never finishes normally.
Decide who owns the pass through the data
For a small, bounded dataset that genuinely needs repeated traversal, materialize it once and share the resulting list:
items = list(value for value in [0, 2, 3])
print(items) # [0, 2, 3]
for item in items:
print(item)
This changes the memory and timing model. All values are collected before processing starts, so it is not the right default for a large file or an unbounded feed. Creating a list also does not deep-copy mutable records. Treat the retained objects consistently if later steps can edit them.
For a stream that only needs one processing pass, I would put counts or bounded diagnostic logging inside that pass instead of adding a preliminary traversal. That keeps the observation attached to the work rather than making it a competing consumer.
Peek at one item and put it back into the pipeline
If the only question is whether a finite or live iterator has a next item, next() with a unique sentinel distinguishes exhaustion from a real value such as zero or None:
from itertools import chain
rows = iter([0, 2, 3])
missing = object()
first = next(rows, missing)
if first is missing:
print('No rows')
else:
rows = chain([first], rows)
print(list(rows)) # [0, 2, 3]
The peek still advances the underlying iterator and may trigger its I/O, computation or other side effects. On a live source it may block while waiting for an item; the sentinel is not a timeout. chain includes that retrieved item in the downstream sequence; it does not undo the original read. Use the reassigned rows afterward, not an old reference to the consumed iterator. The final list is just a small-fixture demonstration, not a requirement for processing a large stream.
Neither iter() nor tee() is a free rewind
Calling iter() on an iterator returns the iterator itself. It does not restore the earlier position. Creating a fresh iterator from a reusable container is different; reopening an external source also needs its own consistency and side-effect checks.
itertools.tee can produce independent iterators, but it may retain substantial buffered data when consumers move at different speeds. It is not a memory-free backup, and its returned iterators are not threadsafe. Choose it only when that tradeoff fits.
What I checked
I ran these synthetic cases in Python 3.10.12. The any(), exhausted-list and one-item restoration results matched the comments. iter(generator) returned that same generator, and a tee branch preserved the fixture for the other branch. No production import, memory benchmark or external feed was tested. Before changing an import, trace every consumer and decide which step is allowed to advance the stream.
Comments
Post a Comment