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.
lst = [1, 2, 3] it = iter(lst) print(next(it)) print(next(it)) print(next(it)) print(next(it))
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.lst = [1, 2, 3] it = iter(lst) print(next(it, "done")) print(next(it, "done")) print(next(it, "done")) print(next(it, "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.def gen(): print("start") yield 1 print("middle") yield 2 print("end") g = gen() # does NOT print "start" print(next(g)) print(next(g))
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.def gen(): yield 1 yield 2 g = gen() print(list(g)) print(list(g)) # second time
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.def countdown(n): while n > 0: yield n n -= 1 for x in countdown(3): print(x)
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.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))
next(gen_exp) computes the first value (0²=0). Generator expressions are memory-efficient for large sequences.print(sum(x**2 for x in range(5)))
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.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])))
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.result = map(str, [1, 2, 3]) print(type(result)) print(list(result)) print(list(result)) # second time
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.a = [1, 2, 3] b = ["a", "b"] # shorter list print(list(zip(a, b)))
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.evens = filter(lambda x: x%2==0, range(10)) print(next(evens)) print(next(evens))
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.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)))
__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.def gen(): yield 1 return "done" # can generators return? yield 2 g = gen() print(next(g)) print(next(g))
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.def gen(): x = yield 1 print(f"received: {x}") yield 2 g = gen() print(next(g)) print(g.send("hello"))
.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.import itertools result = list(itertools.islice(range(1000000), 5)) print(result)
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.nums = [1, 2, 3] doubled = (print(x*2) or x*2 for x in nums) print("Before consuming") list(doubled)
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.