An In-Depth Exploration of Iterators and Generators in Python
Yokeswaran S
Software Engineer @ Tata Communications | Building the AI Product | Sharing Machine Learning Fundamentals | AI Enthusiast
Iterators in Python
Definition
An iterator in Python is an object that allows traversal through elements of an iterable (such as a list or tuple) one at a time. It follows the Iterator Protocol, meaning it must implement two methods:
Example of Iteration in Python
In python we commonly use the loop for the iteration. for example looping through the list
a = [1, 2, 3, 4, 5]
# Using a for loop (Implicit Iteration)
for i in a:
print(i)
Output
1
2
3
4
5
Creating and using an iterator Explicitly
Instead of relying on the for loop we can explicitly create an iterator using the iter() function and manually fetch the elements using next().
a = [1, 2, 3, 4, 5]
# Creating an iterator from the list
iterator = iter(a)
print(type(iterator)) # Output: <class 'list_iterator'>
# Retrieving the first element
b = next(iterator)
print(b)
Output
<class 'list_iterator'>
1
Here what happens step by step:
Accessing all the elements with the next()
To access the remaining elements, we can continue calling next(iterator):
print(next(iterator)) # 2
print(next(iterator)) # 3
print(next(iterator)) # 4
print(next(iterator)) # 5
print(next(iterator)) # Raises StopIteration
Output
2
3
4
5
StopIteration Traceback (most recent call last)
Cell In[9], line 2
1 # if i want to print the element in the iterator we use the next
----> 2 b=next(iterator)
3 print(b) #after the last element printed it return the error
StopIteration:
Generators in python:
A generator in Python is a special type of function that allows you to iterate over data without storing it in memory. Unlike normal functions that return a single value using return, a generator yields multiple values one at a time using the yield keyword.
Generators are lazy iterators, meaning they generate values on the fly only when needed instead of computing everything at once. This makes them useful for handling large datasets efficiently.
How Generators Work – Step by Step Explanation
Let's start with an example.
Example 1: Simple Generator Function
def square(n):
for i in range(3):
yield i**2
# Create a generator object
a = square(3)
print(a) # This prints: <generator object square at 0x...>
# Fetch the first value
print(next(a)) # Output: 0
Step-by-Step Execution Flow
1.Defining the Generator Function
def square(n):
for i in range(3):
yield i**2
2 .Creating the Generator Object
a = square(3)
3. Printing the Generator Object
print(a)
4.Fetching Values Using next()
print(next(a)) # Output: 0
When next(a) is called:
5.Calling next() Again
print(next(a)) #output: 1
6.Final Call to next()
print(next(a)) # Output: 4
7.What Happens If We Call next() Again?
print(next(a)) # Raises StopIteration
Example 2: Understanding the Execution Flow of a Generator
Now, let's take another example that prints messages to show exactly when the generator starts, pauses, and resumes.
def my_generator():
print("Generator started")
yield 1
print("Yielded 1, resuming…")
yield 2
print("Yielded 2, resuming…")
yield 3
print("Yielded 3, resuming…")
# Create the generator (nothing runs yet!)
gen = my_generator()
print(next(gen)) # First call
print(next(gen)) # Second call
print(next(gen)) # Third call
print(next(gen)) # Fourth call (raises StopIteration)
When we define the function my_generator(), it doesn't execute immediately; instead, it prepares a generator that will yield values on demand. Upon creating the generator object with gen = my_generator(), nothing runs yet. The first call to next(gen) starts the execution of the generator. At this point, the message "Generator started" is printed, and the function reaches the first yield 1, which pauses execution and returns 1.
When we call next(gen) again, the function resumes right after the first yield statement. It prints "Yielded 1, resuming…" before reaching the next yield, which is yield 2, returning 2 and pausing again.
Upon the next call to next(gen), the function continues from where it left off, printing "Yielded 2, resuming…" before yielding 3. Finally, if we call next(gen) one more time, the function attempts to resume execution but finds no more values to yield, resulting in a StopIteration exception, indicating that the generator has finished executing.
Expected Output:
Generator started
1
Yielded 1, resuming…
2
Yielded 2, resuming…
3
Yielded 3, resuming…
Traceback (most recent call last):
...
StopIteration
Note: The output is combined here for clarity; in practice, each call executes step by step.
Practical Application
Applications of Generators
Generators are widely used in real-world scenarios, such as:
1. Processing Large Files Line by Line
def read_large_file(filename):
with open(filename, "r") as file:
for line in file:
yield line.strip() # Yield each line one by one
This avoids loading the entire file into memory.
2. Generating Infinite Sequences
def infinite_counter():
num = 0
while True:
yield num
num += 1
This generates numbers forever without running out of memory.
Takeaways
instead of returning a single value.
to iterate over collections of items, such as in a for loop.
one item in memory, unlike lists which store all elements.
function's state until the next value is needed.
and pipelines that transform data through a series of steps.
Summary: Contrasting Iterators and Generators
Iterators:
Generators:
Key Takeaway:
While every generator is an iterator, not every iterator is a generator. Generators provide a concise and efficient way to build iterators, especially useful for handling large datasets or infinite sequences.
Conclusion
Iterators and generators are essential tools in Python that allow for efficient data processing. While iterators provide a standard way to traverse elements in a collection, generators offer a more concise and memory-efficient means of producing data on the fly. By understanding and utilizing these concepts, you can write cleaner, more efficient code, especially when dealing with large or infinite data sets.
Lead- SDET at Tata Communications
5 天前Very clean and crispy
Student
1 周Useful tips! ??
SDE at Altair | Javascript , ReactJS , NodeJS | Undergrad from Rajalakshmi Engineering College
1 周Very informative!
Attended Rajalakshmi Engineering College
1 周Very helpful
FTE @ Geodis India Pvt. Ltd.
1 周Nice explanation