
Generator and Iterator In Python
Generator and Iterator in Python – Learn how generators and iterators work in Python to handle sequences, save memory, and improve code efficiency with examples.
What is an Iterator in Python?
Definition:
An iterator is an object that allows sequential traversal through a collection of data (like lists, tuples, etc.) without indexing.
To be an iterator, an object must implement two methods:
- • __iter__() — returns the iterator object itself.
- • __next__() — returns the next item from the data source. Raises StopIteration when there are no more items.
Why Use Iterators?
- • To loop through data without needing indexing.
- • To abstract data traversal logic (especially for custom data structures).
- • To create your own iteration behavior.
Benefits of Iterators:
- 1. Flexibility: You can define how to iterate over an object (e.g., skip values, filter them).
- 2. Memory Efficiency: Doesn't need to load the entire data structure in memory.
- 3. Custom Behavior: You control the logic behind each next() call.
Example:
class EvenNumbers:
def __init__(self, max):
self.num = 0
self.max = max
def __iter__(self):
return self
def __next__(self):
if self.num > self.max:
raise StopIteration
else:
val = self.num
self.num += 2
return val
for i in EvenNumbers(10):
print(i)
2. What is a Generator in Python?
Definition:
A generator is a simplified iterator created using:
- • a function with the yield keyword, or
- • a generator expression (like list comprehensions but with ())
A generator automatically handles iteration protocol (__iter__() and __next__()), and saves state between calls.
Why Use Generators?
- • To write iterators more easily with less code.
- • To generate large (or infinite) sequences without loading everything in memory.
- • To pause and resume function execution, making them ideal for streaming data.
Conceptual Difference
| Feature | Iterator | Generator |
|---|---|---|
| How it's defined | With a class using __iter__, __next__ | With a function using yield |
| Complexity | More complex, requires boilerplate | Simple, concise |
| Memory usage | It can be high if not managed properly | Very low due to lazy evaluation |
| Control over the state | Full manual control | Automatic state retention between calls |
| Performance | Slightly slower due to overhead | Faster and lightweight in most cases |
Explore Other Demanding Courses
No courses available for the selected domain.
Create a Generator in Python
Creating a generator in Python is as simple as defining a function with at least one yield statement. When called, this function doesn’t return a single value; instead, it returns a generator object that supports the iterator protocol.
The generator has the following syntax in Python:
def generatorname(parameters):
# block of code
Example:
def my_generator():
yield 1
yield 2
yield 3
gen = my_generator()
for value in gen:
print(value)
Output:
1
2
3
What are the benefits of using generators in Python?
- Memory Efficiency: Generators generate values on the fly, reducing memory usage compared to storing the entire sequence in memory.
- Lazy Evaluation: Values are computed only when needed, which can lead to performance improvements, especially with large datasets.
- Maintain State: Generators automatically maintain their state between yield statements, making them suitable for iterative processes.
- Convenience: Generators simplify code for iterating over large datasets or streams of data without needing to manage state explicitly.
How to use the yield statement in generators?
The yield statement is used in a generator function to return a value and pause the function’s execution, preserving its state. When next() is called on the generator, execution resumes right after the yield.
Yield vs Return
yield is used in generator functions to provide a sequence of values over time. When yield is executed, it pauses the function, returns the current value, and retains the state of the function. This allows the function to continue from the same point when called again, making it ideal for generating large or complex sequences efficiently.
return, on the other hand, is used to exit a function and return a final value. Once the return is executed, the function is terminated immediately, and no state is retained. This is suitable for cases where a single result is needed from a function.
Do visit our channel to know more: SevenMentor