Pylerno
English

Iterators

Trace a one-pass source and recognize when it has been exhausted.

On this page

The concept

An iterable can supply an iterator. An iterator remembers its current position and supplies the next item through next. Exhaustion raises StopIteration unless a default is supplied to next. A for loop manages that mechanism for you.

A list can be iterated again, but an existing exhausted iterator does not rewind itself. Functions such as zip, map, and filter return iterators. Consuming one for debugging can remove the values a later computation expected to use.

Worked example

python
numbers = [10, 20, 30]
iterator = iter(numbers)
print(next(iterator))
print(next(iterator))
print(list(iterator))
print(next(iterator, "Exhausted"))
print(list(iterator))
print(list(numbers))

Trace the result

  1. Two next calls consume 10 and 20.
  2. list(iterator) collects only the remaining 30.
  3. A second collection is empty, while the original numbers list still contains all three values.
text
10
20
[30]
Exhausted
[]
[10, 20, 30]
Line-by-line walkthrough

Line 1: numbers = [10, 20, 30]

Evaluate [10, 20, 30], then bind or update numbers with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.

Line 2: iterator = iter(numbers)

Evaluate iter(numbers), then bind or update iterator with the result. The call finishes before its return value is bound; a failed call does not produce a successful assignment. Assignment itself does not imply a deep copy.

Line 3: print(next(iterator))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 4: print(next(iterator))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 5: print(list(iterator))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 6: print(next(iterator, "Exhausted"))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 7: print(list(iterator))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 8: print(list(numbers))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Common mistake

Do not assume a single iterator can be used for two independent passes. Recreate it or deliberately materialize its values.

Check your understanding

What does this program print?

python
it = iter([4, 9])
next(it)
print(next(it))
Choose one answer

Search documentation

Share this page