Generators | Dataplexa

Generators in Python

Generators are special functions that return values one at a time instead of all at once. They allow Python to handle large or infinite sequences efficiently without storing everything in memory.

Generators look similar to normal functions but use the yield keyword. This makes them pause after producing a value and resume when the next value is requested.

Why Do We Need Generators?

Generators help save memory by producing values only when needed instead of creating large lists. They are extremely useful when processing files, streams, and data pipelines where efficiency matters.

Example: Regular Function vs Generator

A normal function returns all results at once. A generator returns one item at a time during iteration.

def normal_function():
    return [1, 2, 3]

def generator_function():
    yield 1
    yield 2
    yield 3

How Generators Work

Generators use yield instead of return, which makes execution pause instead of finishing. Each time the generator is called again, Python continues from the paused point.

Basic Generator Example

def count_to_three():
    yield 1
    yield 2
    yield 3

for num in count_to_three():
    print(num)

This prints values one at a time as the loop requests them from the generator.

Using next() With Generators

You can manually request values from a generator using next(). This allows you to control the flow of iteration step-by-step.

gen = count_to_three()

print(next(gen))
print(next(gen))
print(next(gen))

Generators With Loops

Generators can create sequences using loops instead of multiple yield statements. This helps produce large ranges without creating big lists in memory.

Example: Generator With a Loop

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

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

Real-World Example: Reading a File Line by Line

Generators make file handling efficient by loading one line at a time instead of the whole file. This avoids memory issues when working with large text files or logs.

def read_file(path):
    with open(path, "r") as file:
        for line in file:
            yield line

Generator Expressions

A generator expression looks like a list comprehension, but uses parentheses instead of brackets. It creates values lazily and is more memory-efficient than building a list.

Example of a Generator Expression

gen = (x * 2 for x in range(5))
for value in gen:
    print(value)

Difference Between Generators and Lists

A list stores all values in memory, while a generator produces values only when needed. This makes generators faster and more efficient for large or infinite sequences.

Lists Generators
Store all data at once Produce one value at a time
Use more memory Very memory-efficient
Faster for small data Best for large data or streams

📝 Practice Exercises


Exercise 1

Create a generator that yields numbers from 1 to 5.

Exercise 2

Write a generator that yields even numbers up to 20.

Exercise 3

Build a generator expression that returns squares of numbers from 1 to 10.

Exercise 4

Create a countdown generator starting from 8 to 1.


✅ Practice Answers


Answer 1

def gen_numbers():
    for i in range(1, 6):
        yield i

for n in gen_numbers():
    print(n)

Answer 2

def even_numbers():
    for i in range(2, 21, 2):
        yield i

for n in even_numbers():
    print(n)

Answer 3

squares = (x*x for x in range(1, 11))
for value in squares:
    print(value)

Answer 4

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

for num in countdown():
    print(num)