Python Generators

3 min read ·

Generators are a special type of function that return values one at a time instead of returning all values at once.
Unlike normal functions, generators do not store the entire result in memory.
They generate values only when needed.
This makes generators memory efficient and suitable for large data processing.

Why Generators are Needed

Normal functions return all results at once.
For large data this causes High memory usage Slow performance
Generators solve this problem by producing values lazily.
Lazy means values are generated only when requested.

Difference Between Function and Generator

A normal function uses return and ends execution.
A generator uses yield and pauses execution.
Execution resumes from the same point when next value is requested.

Simple Generator Example

Each next call resumes execution from the last yield.

Understanding yield Keyword

yield returns a value and pauses the function.
The function state is saved automatically.
Next time the function resumes from the same line.
Important

A generator remembers its execution state.

Generator Execution Flow

The loop stops automatically when generator finishes.

Generator vs List

List Stores all values in memory Fast access High memory usage
Generator Produces values on demand Low memory usage One time iteration
Pro Tip

Use generators when working with large datasets or streams.

Generator Expression

Generator expressions are similar to list comprehensions.
They use parentheses instead of square brackets.
Values are generated one by one.

Generator Expression vs List Comprehension

List comprehension creates all values immediately.
Generator expression creates values lazily.

Using Generators with for Loop

The loop fetches values until generator is exhausted.

Infinite Generators

Generators can run infinitely.
Use infinite generators carefully.
Caution

Infinite generators must always have a stopping condition in usage.

Generator with return Statement

Using return stops the generator.
Only one value is produced.

Handling StopIteration

When a generator finishes, Python raises StopIteration internally.
This is handled automatically in loops.

Passing Values to Generators

Generators can receive values using send.

Generator Use Cases

Reading large files line by line Processing streams of data Producing infinite sequences Memory optimized iteration
Real World Scenario

Reading log files of millions of lines using generators avoids memory crashes.

Generators vs Iterators

Iterator Implements __iter__ and __next__ Manual implementation
Generator Simpler syntax Automatic iterator behavior
Generators are easier to write and maintain.