Behind the Scene of Python Loops
Have you ever wondered how Python loops do their thing? Well, I'll summarise it in this blog in simple terms. We'll peek under the hood to see how Python makes those loops work like magic.
Important Terms
Itertool
Itertools is a Python module that provides functions for generating specialized iterators, allowing for efficient and flexible iteration over various data structures. These iterators, created using Itertools functions, can then be iterated over using loops, enabling streamlined and concise handling of complex iteration tasks
Iterable Object
In Python, an iterable is any object capable of returning its members one at a time. It can be thought of as a sequence of elements that can be iterated over, allowing you to access each element in the sequence individually.
Examples: List, Tuple, String, Dictionaries and Generators.iter()
python iter() method returns the iterator object, it is used to convert an iterable to the iterator.
next()
In Python, the
next()
method is a special method used for implementing iterators. It allows an object to be an iterator and defines what happens when you call thenext()
function on that object.Behind the scenes, when you call
next()
on an iterator object, Python internally calls the__next__()
method of that object. This method should return the next item in the sequence, or raise anStopIteration
exception if there are no more items to be returned.
Demystifying Iterators in Python: Understanding Custom Iterators with Visual Explanations
In this diagram, we have Itertool
sent an iter()
function to the iterable object. This iterable object returns the first memory location of that object and the next()
function. If we receive a response __next__()
it means we have other elements in the iterable object. We call the next()
function repeatedly until the StopIteration
exception is raised.
Python Iterate List using iter Function
In this example, we will take a Python List and use the iter() function on it. Then convert it into an iterator and print its value using the __next__() function.
In the last iteration, we again try to print the values using the next() function. But this time it generated an error because in the last iteration, the iterator has already reached its limit and there are no more values in it to print.
When you call next()
on the iterator (myList
), it doesn't change the memory address of the iterator itself. Instead, it moves the iterator to the next item in the sequence ([1, 2, 3, 4, 5]
) and returns that item. So, each time you call, you're getting the next item in the sequence without changing the memory address of the iterator.
myList = [1,2,3,4,5]
myList = iter(myList)
for i in range(0,5):
print(myList)
print(myList.__next__())
Expected Output:
Conclusion:
This blog has introduced the concept of iterators in Python, essential tools for efficiently navigating and processing data. Understanding iterators is key to effectively iterating over sequences in Python code. As I embark on my blogging journey, your feedback is invaluable. Feel free to share any suggestions for improvement or topics you'd like to see covered in future posts. Thank you for joining me on this exploration of Python iterators!
Subscribe to my newsletter
Read articles from Rajvardhan Chavan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by