yield keyword. They provide a memory-efficient way to process data without loading everything into memory at once.
Learning Objectives
After completing this lesson, you will be able to:- Understand iterables, iterators, and generators.
- Create iterators using
iter()andnext(). - Build custom iterators.
- Create generators using
yield. - Differentiate between
yieldandreturn. - Create generator expressions.
- Compare iterators and generators.
- Identify real-world use cases of generators.
What is an Iterator?
An iterator is an object that returns one element at a time from a collection. It remembers its current position and produces the next value only when requested. Python uses iterators internally whenever you iterate over a collection using afor loop.
Iterator Protocol
An iterator implements the following special methods:__iter__()– Returns the iterator object.__next__()– Returns the next element.
__next__() raises a StopIteration exception.
Creating an Iterator
Use theiter() function to create an iterator from an iterable.
Retrieving Values
Use thenext() function to retrieve values from an iterator.
StopIteration
Once all elements are consumed, callingnext() again raises a StopIteration exception.
Practice
Exercise 1
Predict the output.Solution
Solution
Exercise 2
Predict the output.Solution
Solution
iter().Exercise 3
What exception will be raised by the following code?Solution
Solution
A
StopIteration exception is raised because the iterator has no more elements to return.Creating a Custom Iterator
You can create your own iterator by implementing the__iter__() and __next__() methods.
__iter__()returns the iterator object.__next__()returns the next value.- When all values are consumed,
__next__()raises aStopIterationexception.
Example
How It Works
- The
Counterobject is created. - The
forloop calls__iter__()to obtain the iterator. - The loop repeatedly calls
__next__(). - Each call returns the next value.
- When the limit is reached,
StopIterationis raised, ending the loop.
Practice
Exercise 1
Predict the output.Solution
Solution
1 to 3 and then raises StopIteration.Exercise 2
What happens ifraise StopIteration is removed from the __next__() method?
Solution
Solution
The iterator will never indicate that it has finished, causing the loop to continue indefinitely or resulting in incorrect behavior.
Exercise 3
Which two special methods must every custom iterator implement?Solution
Solution
Every custom iterator must implement:
__iter__()__next__()
What is a Generator?
A generator is a special type of iterator created using a function that contains theyield keyword. Unlike a normal function that returns all values at once, a generator produces one value at a time and automatically remembers its execution state.
Generators are easier to write than custom iterators because Python automatically implements the iterator protocol for you.
Creating a Generator
A function becomes a generator as soon as it contains ayield statement.
Using next() with a Generator
The next() function starts the generator and retrieves one value at a time.
"Ending" message is not printed because the generator pauses after the third yield. It executes the remaining statements only when resumed again.
Using a Generator with a for Loop
Generators can be directly used in a for loop.
for loop automatically calls next() until the generator raises StopIteration.
Practice
Exercise 1
Predict the output.Solution
Solution
next() returns the next value produced by the generator.Exercise 2
Predict the output.Solution
Solution
"Hello" message is printed only when the generator starts executing (for example, by calling next(g) or iterating over it).Exercise 3
What is the output?Solution
Solution
for loop automatically retrieves values from the generator until it is exhausted.Understanding yield
The yield keyword is used to produce a value from a generator. Unlike return, which terminates a function, yield pauses the function and preserves its current state. The next time the generator is resumed, execution continues from the statement immediately after the previous yield.
yield vs return
Execution Flow
yield.
State Preservation
One of the biggest advantages of generators is that they automatically preserve the values of local variables.count is not reinitialized each time. Its value is preserved between successive calls to next().
Multiple yield Statements
A generator can contain multiple yield statements.
yield produces one value before the generator pauses.
Practice
Exercise 1
Predict the output.Solution
Solution
OutputThe generator pauses after each
yield. Since the generator is not resumed again, "C" is not printed.Exercise 2
Predict the output.Solution
Solution
OutputThe value of
x is preserved between the two yield statements.Exercise 3
What is the main difference betweenreturn and yield?
Solution
Solution
returnterminates the function and returns a value.yieldpauses the function, returns a value, preserves its state, and resumes execution when requested again.
Generator Expressions
A generator expression provides a concise way to create generators. It is similar to a list comprehension but uses parentheses() instead of square brackets [].
Generator expressions generate values only when required, making them memory efficient.
Syntax
Example
Generator Expression vs List Comprehension
- A list comprehension stores all values in memory.
- A generator expression generates values one at a time.
Practice
Exercise 1
Predict the output.Solution
Solution
next() computes the next value in the sequence.Exercise 2
Which symbol is used to create a generator expression?Solution
Solution
Generator expressions use parentheses
(), whereas list comprehensions use square brackets [].Infinite Generators
Generators can produce infinite sequences because values are generated only when requested.Example
Practice
Exercise 1
Predict the output.Solution
Solution
Fibonacci Generator
Generators are commonly used to generate mathematical sequences.Example
Practice
Exercise 1
What is the first value produced by the generator?Solution
Solution
The first value is 0, because the generator yields
a before updating its value.Memory Efficiency
One of the biggest advantages of generators is memory efficiency.List Example
Generator Example
When to Use Generators
Use generators when:- Working with large datasets.
- Reading large files.
- Processing streaming data.
- Producing values on demand.
- Creating infinite sequences.
Practice
Exercise 1
Which consumes less memory?Solution
Solution
The generator expression consumes significantly less memory because values are generated only when needed.
Iterator vs Generator
Remember: Every generator is an iterator, but not every iterator is a generator.
Iterable vs Iterator vs Generator
- Iterable → An object that can produce an iterator.
- Iterator → Produces one value at a time.
- Generator → A special iterator created using the
yieldkeyword.
Real-World Applications
Generators are commonly used for:- Reading large files line by line.
- Processing large datasets.
- Streaming data from APIs.
- Log processing.
- Data pipelines.
- Machine learning workflows.
- Infinite sequences.
Example: Reading a File
Key Takeaways
- An iterable is an object that can produce an iterator.
- An iterator returns one value at a time using
next(). - A generator is a simpler way to create an iterator using
yield. - The
yieldkeyword pauses execution and preserves the function’s state. - Generator expressions provide a concise syntax for creating generators.
- Generators are ideal for processing large datasets because they use lazy evaluation.
- Every generator is an iterator, but not every iterator is a generator.
Check Your Understanding
Question 1 What is the purpose of theiter() function?
Solution
Solution
The
iter() function converts an iterable into an iterator.Solution
Solution
__iter__() and __next__()yield keyword?
Solution
Solution
The
yield keyword pauses a generator, returns a value, preserves its state, and resumes execution from the same point when requested again.yield and return?
Solution
Solution
returnterminates the function.yieldpauses the function and allows it to continue later.
Solution
Solution
A generator expression is a concise way to create a generator using parentheses
().Solution
Solution
Generators create values only when they are requested instead of storing all values in memory.
for loop?
Solution
Solution
Yes. A generator is an iterator and can be directly used in a
for loop.Solution
Solution
False. Every generator is an iterator, but not every iterator is a generator.
Solution
Solution
Examples include:
- Reading large files
- Processing large datasets
- Streaming API data
- Log processing
- Infinite sequences
Practice & Exercises
To reinforce what you’ve learned in this section (Iterators, Custom Iterators, Generators, and Generator Expressions), practice with these interactive notebooks:Follow-Along Practice
Practice creating iterators, implementing custom iterator classes, writing generators with yield, and creating generator expressions.💻 VS Code | 🚀 Colab | 📥 Download
Practice Exercises
Test your skills with exercises on custom range iterators, cubes generators, odd numbers generator expressions, and infinite powers of three generators.💻 VS Code | 🚀 Colab | 📥 Download