šŸ

Generators and Iterators

Advanced
120 XP
50 min
Lesson Content

Generators: Memory-Efficient Iteration

Generators are a special type of iterator that generate values on-the-fly, making them memory-efficient for large datasets.

What are Generators?

Generators produce values one at a time, only when needed. They don't store all values in memory.

Generator Functions

# Regular function (creates entire list in memory)
def squares_list(n):
    result = []
    for i in range(n):
        result.append(i ** 2)
    return result

# Generator function (yields one at a time)
def squares_generator(n):
    for i in range(n):
        yield i ** 2

# Usage
for square in squares_generator(5):
    print(square)  # 0, 1, 4, 9, 16

Generator Expressions

# Similar to list comprehension, but with parentheses
squares = (x ** 2 for x in range(5))

# Memory efficient!
for square in squares:
    print(square)

# Can be used with functions that take iterables
sum_of_squares = sum(x ** 2 for x in range(10))

Why Use Generators?

  • āœ… Memory Efficient: Don't store all values
  • āœ… Lazy Evaluation: Values generated on demand
  • āœ… Infinite Sequences: Can generate infinite data
  • āœ… Performance: Faster for large datasets

Common Patterns

# Infinite generator
def count_up():
    count = 0
    while True:
        yield count
        count += 1

# Fibonacci generator
def fibonacci():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

# Reading large files line by line
def read_large_file(filename):
    with open(filename) as f:
        for line in f:
            yield line.strip()

Generator Methods

gen = (x for x in range(5))

# Get next value
print(next(gen))  # 0
print(next(gen))  # 1

# Send value to generator
def receiver():
    while True:
        value = yield
        print(f"Received: {value}")

rec = receiver()
next(rec)  # Start generator
rec.send("Hello")  # Received: Hello
Example Code

Create generator functions for different sequences.

# Generator for even numbers
def even_numbers(n):
    for i in range(n):
        if i % 2 == 0:
            yield i

# Use the generator
for num in even_numbers(10):
    print(num)

# Generator expression for squares
squares = (x ** 2 for x in range(5))
print(list(squares))

Expected Output:

0
2
4
6
8
[0, 1, 4, 9, 16]
Study Tips
  • •Read the theory content thoroughly before practicing
  • •Review the example code to understand key concepts
  • •Proceed to the Practice tab when you're ready to code