What are generators and how do I write them in Python?
· Category: Python Programming
Short answer
Generators are iterators defined with functions containing yield or with generator expressions. They produce values lazily, one at a time, making them memory-efficient for large datasets.
Steps
- Write a function that uses
yieldinstead ofreturn. - Call the function to get a generator object.
- Iterate over it or use
next()to retrieve values.
def count_up_to(max_val):
count = 1
while count <= max_val:
yield count
count += 1
counter = count_up_to(5)
for num in counter:
print(num)
Tips
- Generator expressions
(x for x in iterable)are like list comprehensions but lazy. - Generators maintain state between
yieldcalls and resume execution on the next iteration. - Use
yield fromto delegate to another iterable (Python 3.3+).
# Generator expression
squares = (x**2 for x in range(1000000))
print(next(squares))
# yield from
def combined():
yield from range(3)
yield from ["a", "b"]
print(list(combined()))
Common issues
- Once a generator is exhausted, iterating over it again produces no values; recreate it if needed.
- Mixing
returnwithyieldin Python 3 returns a value that can be captured withStopIteration.value. - Generators are single-use iterators, not reusable collections.