Skip to content

Iterators in Python

An iterator is an object that produces a sequence of values one at a time. Python’s for loop works by calling next() on an iterator until it raises StopIteration.

flowchart TB
Iterable["📦 Iterable
has __iter__()
list, tuple, str, dict"] -->|"iter(iterable)"| Iterator["🔄 Iterator
has __iter__() + __next__()
remembers its position"]
Iterator -->|"next(iterator)"| Value["📤 Returns next value
1, 2, 3, ..."]
Value -->|"More values?"| Iterator
Value -->|"No more values!"| Stop["⛔ StopIteration
Exception raised"]
style Iterable fill:#7c3aed,color:#fff
style Iterator fill:#4f46e5,color:#fff
style Value fill:#059669,color:#fff
style Stop fill:#dc2626,color:#fff
# An object is iterable if it has __iter__() that returns an iterator
# An object is an iterator if it has __next__() that returns values
numbers = [1, 2, 3]
it = iter(numbers) # Same as numbers.__iter__()
print(next(it)) # 1
print(next(it)) # 2
print(next(it)) # 3
# print(next(it)) # StopIteration!
class Countdown:
def __init__(self, start):
self.start = start
def __iter__(self):
self.current = self.start
return self
def __next__(self):
if self.current <= 0:
raise StopIteration
value = self.current
self.current -= 1
return value
for num in Countdown(5):
print(num) # 5, 4, 3, 2, 1
# enumerate — indexed iteration
for i, char in enumerate("hello"):
print(i, char)
# zip — parallel iteration
for a, b in zip([1, 2, 3], ['a', 'b', 'c']):
print(a, b)
# reversed — reverse iteration
for char in reversed("hello"):
print(char)
# iter with sentinel
def read_until_empty():
while True:
line = input("> ")
if line == "":
break
yield line
# itertools module
from itertools import cycle, chain, islice, count
# Infinite cycle
colors = cycle(["red", "green", "blue"])
for _ in range(6):
print(next(colors)) # red, green, blue, red, green, blue
# Chain iterables
combined = chain([1, 2, 3], ['a', 'b', 'c'])
print(list(combined)) # [1, 2, 3, 'a', 'b', 'c']
# Infinite count
for i in islice(count(10, 2), 5):
print(i) # 10, 12, 14, 16, 18
  1. Use iterators for large datasets — they’re memory-efficient
  2. Use iter() and next() for manual iteration control
  3. Use itertools for common iteration patterns
  4. Know when an iterator is exhausted — you can’t reset it

Exercise 1: Create an iterator that generates Fibonacci numbers up to a limit.

Exercise 2: Implement an iterator that reads a file in chunks of N bytes.