Content · Programming · Python

Python Avoid Exhausting a Generator Accidentally

Avoid consuming an iterator once for a check and finding it empty on the second pass.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Avoid consuming an iterator once for a check and finding it empty on the second pass.

What You Will Learn


Generators and most iterator objects are one-pass streams. Calls such as list(), sum(), any(), or a for loop can consume values, changing what remains. If you need multiple passes, create a fresh generator each time or store a bounded small input as a list.

Many iterator tools are lazy. Creating the iterator does not necessarily perform the work; advancing it can consume the original source.

Step-by-Step Example


The example below makes the behavior visible with small input values that you can change safely.

numbers = (value * 2 for value in range(4))
print(next(numbers))
print(list(numbers))
print(list(numbers))

def make_numbers():
    return (value * 2 for value in range(4))

print(list(make_numbers()))

How the Example Works


  • The function make_numbers defines reusable behavior; its body executes when it is called, not when it is declared.
  • The variable numbers holds the result of (value * 2 for value in range(4)). Later lines use that value or inspect how it changes.
  • The print() calls make the return values or state visible so you can compare them with the explanation and check your prediction.

Expected output from the example:

0
[2, 4, 6]
[]
[0, 2, 4, 6]

When You Would Use This


This pattern is useful in readers for CSV files, event streams, or paginated API results, where collecting everything in memory is unnecessary. Because iterator state advances when values are consumed, decide whether you need a reusable collection or a one-time pass. Keep files and connections open until their lazy consumers finish.

Practice: Modify the Code


Show how an early break leaves the remaining values available but does not restart the iterator.

Verify the changed output against the behavior described above, including the relevant invalid or boundary case.

For complete parameters and version details, consult the official Python documentation.

Common Beginner Mistakes


  • Calling any(iterator) to check its contents and expecting the stream position to remain unchanged.
  • Trying to restart a generator using iter(generator), which returns the same exhausted iterator.

Key Takeaways


  • Avoid consuming an iterator once for a check and finding it empty on the second pass.
  • Generators and most iterator objects are one-pass streams.
  • Apply this operation only after checking the assumptions and input conditions shown in the example.
Share this resourceSend this page to someone who may find it useful.