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

  1. Write a function that uses yield instead of return.
  2. Call the function to get a generator object.
  3. 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 yield calls and resume execution on the next iteration.
  • Use yield from to 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 return with yield in Python 3 returns a value that can be captured with StopIteration.value.
  • Generators are single-use iterators, not reusable collections.