Chapter 11

Iterators & Generators

Generators are lazy — they produce values one at a time. Once exhausted, they're empty forever. yield is not return. These are deep Python concepts.

Questions: 16
Difficulty: ★★★★☆
Generator exhaustion: Generators can only be iterated once. Once all values are produced, the generator is empty — iterating again gives nothing. This surprises many people who treat generators like lists.
Iterators
Q 01Predict the Output
lst = [1, 2, 3]
it = iter(lst)
print(next(it))
print(next(it))
print(next(it))
print(next(it))
Output
1 2 3 StopIteration
Why? iter(lst) returns an iterator object. next(it) yields the next value. When exhausted, next() raises StopIteration. A for loop automatically calls iter() and next() and handles StopIteration — that's how loops work under the hood.
Q 02Multiple Choice
lst = [1, 2, 3]
it = iter(lst)
print(next(it, "done"))
print(next(it, "done"))
print(next(it, "done"))
print(next(it, "done"))
Answer: a) 1, 2, 3, "done".

next(iterator, default) — the second argument is a default value to return when the iterator is exhausted instead of raising StopIteration. This makes safe iteration easier without try/except blocks.
Generators — yield
Q 03Predict the Output
def gen():
    print("start")
    yield 1
    print("middle")
    yield 2
    print("end")

g = gen()       # does NOT print "start"
print(next(g))
print(next(g))
Output
start 1 middle 2
Why? Calling gen() creates a generator object but does NOT run any code — execution is paused before the first line. next(g) runs until the first yield, returns 1, and pauses. The next next(g) resumes from after the first yield, prints "middle", reaches yield 2, returns 2, and pauses again. "end" is never printed here.
Q 04Multiple Choice
def gen():
    yield 1
    yield 2

g = gen()
print(list(g))
print(list(g))   # second time
Answer: b) [1,2], [].

Generators are exhausted after one full iteration. After list(g), the generator is at the end. Calling list(g) again creates an empty list — no error, but nothing to yield. To iterate multiple times, call gen() again to get a fresh generator.
Q 05Predict the Output
def countdown(n):
    while n > 0:
        yield n
        n -= 1

for x in countdown(3):
    print(x)
Output
3 2 1
Why? Each yield n pauses and returns the current value. The for loop calls next() automatically. After yielding, execution resumes after the yield — so n -= 1 runs, then the while check, then yield again. This is lazy — values are computed on demand.
Generator Expressions
Q 06Predict the Output
gen_exp = (x**2 for x in range(5))
list_comp = [x**2 for x in range(5)]
print(type(gen_exp))
print(type(list_comp))
print(next(gen_exp))
Output
<class 'generator'> <class 'list'> 0
Why? Generator expression (parentheses) vs list comprehension (brackets). The list comprehension creates all 25 values in memory immediately. The generator expression creates a lazy iterator — no values computed yet. next(gen_exp) computes the first value (0²=0). Generator expressions are memory-efficient for large sequences.
Q 07Multiple Choice
print(sum(x**2 for x in range(5)))
Answer: b) 30.

0+1+4+9+16=30. sum() accepts any iterable — including a generator expression. When a generator expression is the only argument to a function, you can omit the extra parentheses: sum(x**2 for x in range(5)) instead of sum((x**2 for x in range(5))). Memory efficient for large datasets.
yield from
Q 08Predict the Output
def flat(lst):
    for item in lst:
        if isinstance(item, list):
            yield from flat(item)
        else:
            yield item

print(list(flat([1, [2, [3, 4]], 5])))
Output
[1, 2, 3, 4, 5]
Why? yield from delegates yielding to a sub-iterable (here, a recursive call). It yields all values from the inner generator transparently. This enables elegant recursive flattening. Without yield from, you'd need for x in flat(item): yield x.
map, filter, zip
Q 09Predict the Output
result = map(str, [1, 2, 3])
print(type(result))
print(list(result))
print(list(result))   # second time
Output
<class 'map'> ['1', '2', '3'] []
Why? In Python 3, map() returns a lazy map object (iterator), not a list. After the first list(result) exhausts it, the second call returns an empty list. This is the same exhaustion behavior as generators. filter() and zip() behave the same way.
Q 10Predict the Output
a = [1, 2, 3]
b = ["a", "b"]   # shorter list
print(list(zip(a, b)))
Output
[(1, 'a'), (2, 'b')]
Why? zip() stops at the shortest iterable. Element 3 from a has no partner in b, so it's dropped. Use itertools.zip_longest(a, b, fillvalue=None) if you want to keep all elements and fill the missing ones with a default.
Q 11Multiple Choice
evens = filter(lambda x: x%2==0, range(10))
print(next(evens))
print(next(evens))
Answer: a) 0, 2.

filter() returns a lazy iterator — you can call next() on it. First call gives 0 (first even in range). Second gives 2. The filter checks each element lazily. 0 is even, so it's the first value. range(10) is 0–9.
Iterator Protocol
Q 12Predict the Output
class Counter:
    def __init__(self, low, high):
        self.current = low
        self.high = high

    def __iter__(self): return self

    def __next__(self):
        if self.current > self.high:
            raise StopIteration
        val = self.current
        self.current += 1
        return val

print(list(Counter(1, 3)))
Output
[1, 2, 3]
Why? The iterator protocol requires two methods: __iter__ (returns self) and __next__ (returns next value or raises StopIteration). Implementing these makes any object usable in a for loop or with list(). This is how Python's iteration mechanism works under the hood.
Q 13Multiple Choice
def gen():
    yield 1
    return "done"   # can generators return?
    yield 2

g = gen()
print(next(g))
print(next(g))
Answer: c) 1, StopIteration.

Generators CAN have a return statement (Python 3). But return raises StopIteration — the returned value becomes the exception's value attribute. The second yield 2 is unreachable. The return value "done" is accessible via except StopIteration as e: e.value.
Q 14Predict the Output
def gen():
    x = yield 1
    print(f"received: {x}")
    yield 2

g = gen()
print(next(g))
print(g.send("hello"))
Output
1 received: hello 2
Why? Generators can receive values via .send(value). The sent value becomes the result of the yield expression on the generator's side. First next(g) runs to the first yield and returns 1. Then g.send("hello") resumes, and x = "hello", prints it, runs to the second yield, returns 2.
Q 15Multiple Choice
import itertools
result = list(itertools.islice(range(1000000), 5))
print(result)
Answer: a) [0, 1, 2, 3, 4].

itertools.islice(iterable, n) lazily takes the first n elements from an iterator without consuming the rest. Combined with range(1000000) (already lazy), only 5 values are ever computed. No memory issue. islice is the lazy alternative to list slicing.
Q 16Predict the Output
nums = [1, 2, 3]
doubled = (print(x*2) or x*2 for x in nums)
print("Before consuming")
list(doubled)
Output
Before consuming 2 4 6
Why? The generator expression is lazy — no computation happens until you consume it. "Before consuming" prints first. Then list(doubled) consumes the generator, which triggers the print(x*2) for each element. This demonstrates that generator expressions are truly lazy — the body doesn't run until iterated.
← Chapter 10: Classes & OOP Chapter 12: Exceptions →