0
0
Pythonprogramming~10 mins

Iterator protocol in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Iterator protocol
Create iterable object
Call iter() to get iterator
Call next() on iterator
Yield next item if available
StopIteration raised when done
End
The iterator protocol means an object can be looped over by calling iter() to get an iterator, then repeatedly calling next() until no items remain.
Execution Sample
Python
numbers = [10, 20, 30]
it = iter(numbers)
print(next(it))
print(next(it))
print(next(it))
This code creates an iterator from a list and prints each item one by one using next().
Execution Table
StepActionIterator Statenext() ResultOutput
1Create list 'numbers'N/AN/AN/A
2Call iter(numbers) to get iterator 'it'it points before first elementN/AN/A
3Call next(it)it moves to first element1010
4Call next(it)it moves to second element2020
5Call next(it)it moves to third element3030
6Call next(it) againNo more elementsStopIteration raisedProgram stops if not caught
💡 StopIteration raised because iterator has no more elements
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6
numbers[10, 20, 30][10, 20, 30][10, 20, 30][10, 20, 30][10, 20, 30][10, 20, 30]
it (iterator position)N/ABefore first elementAt 10At 20At 30End (no more elements)
Key Moments - 3 Insights
Why does calling next() after the last item cause an error?
Because the iterator has no more items, next() raises StopIteration to signal the end (see step 6 in execution_table).
Is the original list 'numbers' changed by iter() or next()?
No, the list stays the same; only the iterator's position changes (see variable_tracker for 'numbers' and 'it').
What does iter() actually do?
iter() returns an iterator object that keeps track of the current position for next() calls (step 2 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of next(it) at step 4?
A20
B30
C10
DStopIteration
💡 Hint
Check the 'next() Result' column at step 4 in execution_table.
At which step does the iterator reach the end and raise StopIteration?
AStep 3
BStep 5
CStep 6
DStep 2
💡 Hint
Look for 'StopIteration raised' in the 'next() Result' column.
If we call next(it) only twice, what will be the iterator's position after that?
AAt 10
BAt 20
CBefore first element
DAt 30
💡 Hint
Check variable_tracker for 'it' after step 4.
Concept Snapshot
Iterator protocol in Python:
- Use iter(obj) to get an iterator.
- Use next(iterator) to get next item.
- When no items left, next() raises StopIteration.
- Original object stays unchanged.
- Enables looping over objects step-by-step.
Full Transcript
The iterator protocol in Python allows an object to be looped over by first calling iter() to get an iterator. This iterator remembers the current position. Each call to next() returns the next item and moves the position forward. When there are no more items, next() raises StopIteration to signal the end. The original object does not change during this process. This protocol is how Python loops like for work behind the scenes.