Nearby lessons

115 of 159

Python - Generator Expressions

📌 What You Will Learn
  • 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

🐍Code Cell
1generator = (expression for item in iterable)
Output
No output captured.

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

🐍Code Cell
1numbers = (x for x in range(1, 6))
2 
3print(numbers)
Output
at 0x...>

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

🐍Code Cell
1numbers = (x for x in range(1, 6))
2 
3print(next(numbers))
4print(next(numbers))
5print(next(numbers))
6print(next(numbers))
7print(next(numbers))
Output
1
2
3
4
5

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.

🐍Code Cell
1numbers = (x for x in range(1, 6))
2 
3for value in numbers:
4 print(value)
Output
1
2
3
4
5

Program 1: Generate Squares

🐍Code Cell
1squares = (x ** 2 for x in range(1, 6))
2 
3for value in squares:
4 print(value)
Output
1
4
9
16
25

Program 2: Generate Cubes

🐍Code Cell
1cubes = (x ** 3 for x in range(1, 6))
2 
3print(list(cubes))
Output
[1, 8, 27, 64, 125]

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

🐍Code Cell
1evenNumbers = (
2 x for x in range(1, 11)
3 if x % 2 == 0
4)
5 
6print(list(evenNumbers))
Output
[2, 4, 6, 8, 10]

Program 4: Generate Odd Numbers

🐍Code Cell
1oddNumbers = (
2 x for x in range(1, 11)
3 if x % 2 != 0
4)
5 
6print(list(oddNumbers))
Output
[1, 3, 5, 7, 9]

Program 5: Generate Numbers Greater Than 20

🐍Code Cell
1numbers = [10, 15, 20, 25, 30, 35]
2 
3result = (x for x in numbers if x > 20)
4 
5print(list(result))
Output
[25, 30, 35]

Generator Expression with String Data

Generator Expressions can also transform string data.

🐍Code Cell
1names = ["Rahul", "Amit", "Neha", "Pooja"]
2 
3upperNames = (name.upper() for name in names)
4 
5print(list(upperNames))
Output
['RAHUL', 'AMIT', 'NEHA', 'POOJA']

Generator Expression with a String Condition

🐍Code Cell
1languages = [
2 "C",
3 "Java",
4 "Python",
5 "React",
6 "JavaScript"
7]
8 
9result = (
10 language
11 for language in languages
12 if len(language) > 5
13)
14 
15print(list(result))
Output
['Python', 'JavaScript']

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:

🐍Code Cell
1numbers = [x ** 2 for x in range(1, 6)]
2 
3print(numbers)
Output
[1, 4, 9, 16, 25]

Equivalent Generator Expression

The equivalent Generator Expression uses parentheses instead of square brackets:

🐍Code Cell
1numbers = (x ** 2 for x in range(1, 6))
2 
3print(numbers)
4 
5print(list(numbers))
Output
at 0x...>
[1, 4, 9, 16, 25]

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

🐍Code Cell
1numbers = (x * x for x in range(1000000))
2 
3print(next(numbers))
4print(next(numbers))
5print(next(numbers))
Output
0
1
4

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

🐍Code Cell
1numbers = (x for x in range(1, 4))
2 
3print(list(numbers))
4 
5print(list(numbers))
Output
[1, 2, 3]
[]

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

🐍Code Cell
1result = sum(x for x in range(1, 6))
2 
3print(result)
Output
15

Using max() and min() with a Generator Expression

Generator Expressions work directly with other built-in functions such as max() and min().

🐍Code Cell
1result = max(x ** 2 for x in range(1, 6))
2 
3print(result)
4 
5result = min(x ** 2 for x in range(1, 6))
6 
7print(result)
Output
25
1

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(), and max().
  • 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.
📝 Key Takeaways
  • 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

🧠 Test Your Knowledge

10 Questions
Progress: 0 / 10