Python Documentation

Generators

Generators in Python

Generators in Python are a simple way of creating iterators. They are written like regular functions but use the yield statement instead of return. Generators allow you to generate items only one at a time and only when you ask for it, which is memory efficient for large data sets.

Basic Concept

A generator function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a generator function.

Simple Generator Example

def countdown(n):
while n > 0:
ㅤㅤyield n
ㅤㅤn -= 1

for i in countdown(5):
print(i)

# Output:
# 5
# 4
# 3
# 2
# 1

Generator Expression

Generator expressions are a high performance, memory efficient generalization of list comprehensions and generators.

squares = (x**2 for x in range(10))

forsquare in squares:
print(square)

# Output: 0, 1, 4, 9, 16, 25, 36, 49, 64, 81

Benefits of Generators

  • Memory Efficient: They generate items one at a time and only when you ask for it, so they are memory friendly.
  • Represent Infinite Stream: Generators can represent an infinite stream of data.
  • Pipelining Generators: You can use multiple generators in a pipeline, which is both memory and CPU efficient.

Pipelining Generators Example

def fibonacci():
ㅤa, b = 0, 1
while True:
ㅤㅤyield a
ㅤㅤa, b = b, a + b

def square(nums):
for num in nums:
ㅤㅤyield num ** 2

fib_squares = square(fibonacci())

for iin range(10):
print(next(fib_squares))

# Output: 0, 1, 1, 4, 9, 25, 64, 169, 441, 1156

Generators are a powerful feature in Python that provide an efficient way to work with large or infinite sequences of data. They are widely used in data processing, file I/O operations, and in scenarios where memory efficiency is crucial.