An iterator advances through a sequence and signals completion with StopIteration. An iterable can usually create a fresh iterator.
Before you start
You should know Python functions, collections and exceptions. Use a small isolated script or interactive session to trace the example. Pay attention to when objects are created and when work executes; iteration, binding and mutation can happen at different points in a program.
The practical goal is to reason through this situation: A list supports multiple traversals; an iterator created from it retains its current position. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Distinguish iterable from iterator
A list can create fresh iterators; an iterator holds advancing position.
Step 2: Trace consumption
Next moves the cursor and eventual exhaustion raises StopIteration.
Step 3: Document single-use inputs
Consumers must not silently assume an already-advanced iterator starts from the beginning.
Worked scenario
A list supports multiple traversals; an iterator created from it retains its current position.
values = [1, 2, 3]
iterator = iter(values)
print(next(iterator)) # 1
print(list(iterator)) # [2, 3]
print(list(iterator)) # []
print(list(values)) # [1, 2, 3]The final list traversal creates fresh iteration rather than rewinding the exhausted iterator.
Common mistake
Passing an already consumed iterator can silently yield no data.
Verify the behavior
Pass empty, fresh and partially consumed iterators to the consumer and assert the intended contract.
Interview exercise
Detect a single-use API contract.
Answer and reasoning
Document iterator ownership and test empty and partially consumed input rather than assuming every iterable is replayable.
Continue learning
Compare the scenario with the Python interview questions and test your understanding with the Python MCQs. For terminology and implementation details, consult the reference material.