Nearby lessons
115 of 159Python - Generator Expressions
- Understand what a generator expression is
- Write generator expressions using parentheses
- Add conditions to filter generated values
- Compare generator expressions with list comprehensions
- Use generator expressions with sum(), min(), and max()
What is a Generator Expression?
Python provides a shorter and simpler way to create generators called a Generator Expression.
A Generator Expression is a compact way to create a Generator Object using a single expression.
It does not generate and store all values immediately. Instead, each value is generated only when it is requested.
Generator Expressions are similar to List Comprehensions, but they use parentheses () instead of square brackets [].
Like Generator Functions, Generator Expressions generate values one at a time using lazy evaluation.
Syntax
Explanation of the Syntax
| Part | Description |
|---|---|
expression |
The operation performed on each element. |
item |
The variable representing each element of the iterable. |
iterable |
The sequence or collection being processed. |
() |
Parentheses are used to create a Generator Expression. |
First Generator Expression
Explanation
The expression creates a Generator Object.
The values from 1 to 5 are not immediately stored in memory as a complete collection. Instead, they are generated one at a time when requested.
The exact memory address shown in the Generator Object may be different each time the program runs.
Using next() with a Generator Expression
Using a Generator Expression with a for Loop
A Generator Expression can be directly used with a for loop. The loop retrieves one value at a time and automatically stops when the Generator is exhausted.
Program 1: Generate Squares
Program 2: Generate Cubes
Generator Expression with a Condition
A Generator Expression can include an if condition. The condition determines which elements should be generated.
Program 3: Generate Even Numbers
Program 4: Generate Odd Numbers
Program 5: Generate Numbers Greater Than 20
Generator Expression with String Data
Generator Expressions can also transform string data.
Generator Expression with a String Condition
List Comprehension vs Generator Expression
| List Comprehension | Generator Expression |
|---|---|
Uses square brackets []. |
Uses parentheses (). |
| Creates a List. | Creates a Generator Object. |
| Generates all values immediately. | Generates values only when requested. |
| Stores all values in memory. | Produces values one at a time. |
| Suitable for smaller collections. | Suitable for large sequences. |
| Values can be accessed repeatedly. | Values are consumed during iteration. |
| Supports indexing. | Does not support direct indexing. |
List Comprehension and its Equivalent Generator Expression
Consider the following List Comprehension:
Equivalent Generator Expression
The equivalent Generator Expression uses parentheses instead of square brackets:
Memory Efficiency
The main advantage of Generator Expressions is memory efficiency.
A List Comprehension creates all values and stores them in memory immediately. A Generator Expression creates values only when they are requested.
For a small collection, the difference may not be noticeable. However, when processing millions of values, Generator Expressions can significantly reduce memory usage.
Example: Large Sequence
Explanation
The Generator Expression represents one million square values.
However, all one million values are not created and stored at once. Only the requested values are generated.
This is why Generator Expressions are useful for processing large sequences.
A Generator Expression Can Be Exhausted
Explanation
The first list(numbers) consumes all values from the Generator.
After that, the Generator Object is exhausted, so the second conversion produces an empty list.
To generate the sequence again, a new Generator Expression must be created.
Using sum() with a Generator Expression
Using max() and min() with a Generator Expression
Generator Expressions work directly with other built-in functions such as max() and min().
Explanation
When a Generator Expression is passed as the only argument to a function, an additional pair of parentheses is not required.
The functions consume the generated values and calculate the total, maximum, or minimum.
Generator Function vs Generator Expression
| Generator Function | Generator Expression |
|---|---|
Defined using def. |
Created using expression syntax. |
Uses the yield keyword. |
Does not explicitly use yield. |
| Can contain multiple statements. | Contains a single expression. |
| Suitable for complex generator logic. | Suitable for simple generator logic. |
| Returns a Generator Object. | Creates a Generator Object. |
| Supports lazy evaluation. | Supports lazy evaluation. |
Advantages of Generator Expressions
- Short and concise syntax.
- Memory efficient.
- Supports lazy evaluation.
- Useful for processing large sequences.
- Works directly with functions such as
sum(),min(), andmax(). - Does not require defining a separate Generator Function for simple operations.
Limitations of Generator Expressions
- Values are consumed only once.
- Does not support direct indexing.
- Not suitable for complex multi-statement logic.
- A new Generator Expression must be created after the previous Generator is exhausted.
- Converting the complete Generator into a list removes its main memory-saving advantage.
- A generator expression creates a generator object using a single line of code
- Generator expressions use parentheses () instead of square brackets []
- They generate values lazily, one at a time, saving memory
- An if condition can be added to filter which values are produced
- A generator expression can be used only once and is then exhausted