Iterators
Overview
An Iterator is an object that contains a countable number of values and allows you to traverse them one by one using the next() function. Under the hood, whenever you write a for loop, Python secretly requests an Iterator from the target object and repeatedly calls next() until the values run out. Understanding this protocol allows you to build highly customized, lazy-evaluated data streams.
Syntax
nums = [10, 20, 30] # This is an Iterable
# Extract the Iterator from the Iterable
iterator = iter(nums)
# Traverse manually
print(next(iterator)) # 10
print(next(iterator)) # 20
print(next(iterator)) # 30
# print(next(iterator)) -> Raises StopIteration Exception
# --- Building a Custom Iterator ---
class RangeCounter:
def __init__(self, limit):
self.limit = limit
self.current = 0
def __iter__(self):
return self # Must return itself
def __next__(self):
if self.current >= self.limit:
raise StopIteration # Signals the end of the loop
self.current += 1
return self.current
for num in RangeCounter(3):
print(num) # 1, 2, 3Common Pitfalls
- Confusing Iterables (like Lists or Strings) with Iterators. You can loop over an Iterable infinitely many times. However, Iterators are one-way streets; once they hit
StopIteration, they are permanently exhausted and must be recreated. - Calling
next()manually without a fallback. Usenext(iterator, default_value)to prevent a crash if the iterator is already exhausted.
Interview Questions
An Iterable is an object capable of producing an Iterator (it implements __iter__). An Iterator is the actual worker object that remembers the state and produces the next value (it implements __next__).
Real-World Example
Using next() with a generator expression to safely and efficiently locate the first matching item in a list without scanning the rest.
users = [{"id": 1, "active": False}, {"id": 2, "active": True}]
# Finds the first active user instantly. If none exist, returns None safely.
first_active = next((user for user in users if user["active"]), None)
print(first_active) # {'id': 2, 'active': True}Check Your Knowledge
Test your understanding of Iterators with these quick questions.