This article was published as a part of the Data Science Blogathon
Python is a beautiful Programming Language. Because of its flexibility and incredible functionalities, most people loved this language. While working with Python, I have come across a few functionalities whose usage is not commensurate to the number of complexities they simplify. Sometimes, these are also known as “Hidden Gems” in Python. These hidden things are not known by a lot of people but they become super useful when anyone wants to work in the domain of Analytics and Data Science.
In this category, Python Iterators and Iterables can be fitted. Their potential is immense!
Image Source: Link
One of the most important concepts in Python is Iteration. The term Iteration not comes alone but comes up with two other terms that pertain to iteration are iterators and iterables. Also, the understanding of iterables and iterators in Python is very important. But these python concepts are a little bit tricky to understand. In this article, I will try to give a comprehensive explanation of iterables and iterators. We will understand these things starting from what they are, how they work, and also see how to use them properly.
So let’s deep dive into the article and explore the world of Python Iterators and Iterables.
To follow this article properly, I assume you are familiar with the basics of Python. If not, I recommend the below popular course given by Analytics Vidhya to get started with the Python Basics:
One of the most essential principles of software development is that Don’t Repeat Yourself, which is elaborated in “The Pragmatic Programmer” Book in the following way:
“Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.” — Andy Hunt and Dave Thomas
One specific application of the above-mentioned principle in modern programming is the use of Iteration that involves going over a list of items, on which we performed the defined operation. One of the most basic forms of iteration is a for loop.
Although many other languages such as Swift and JavaScript use three-expression for-loop, while Python programming language uses a more concise syntax of for-loop.
To understand what exactly iteration means, you have to understand the following points:
Iterables are objects that can be iterated in iterations.
To understand what exactly iterables means, you have to understand the following points:
An object is called an iterable if you can get an iterator out of it.
A simpler way to determine whether an object is iterable is to check if it supports __iter__. How? Here we use the function named dir( ), which returns the list of attributes and methods supported by an object, and by seeing all attributes and methods, we can find all and select required methods from them.
Image Source: Link
For Example,
Python Code:
# String as an iterable
for i in 'chirag':
print(i)
print('-----')
# list as an iterable
for i in [1, 2, 3]:
print(str(i*2))
An Iterator is an object representing a stream of data that produces a data value at a time using the __next__() method.
To understand what exactly iterators mean, you have to understand the following points:
Iterator supports in-built dunder methods such as __iter__ and __next__
Iterators can only move forward using __next__. But remember that, iterators cannot go back or cannot be reset.
To learn more about the dunder methods in Python, refer to the link
Let’s learn more about the __next__ function:
For Example,
number_iterator = iter([1, 2, 3, 4, 5]) print(type(number_iterator)) print(next(number_iterator)) print(next(number_iterator)) print(next(number_iterator)) print(next(number_iterator)) print(next(number_iterator)) # Once the iterator is exhausted, next() function raise StopIteration. print(next(number_iterator))
Output:
Specifically, in the above code, we first created an iterator called number_iterator by passing a list of numbers to the iter() method. When we checked its type with the help of the type() function of Python, we found that it was indeed an iterator, or more specifically a list_iterator. Every time we called the next() method on the number_iterator, the iterator produced an integer for us until the StopIteration exception is raised.
In modern days, we have a lot of data in our hands, and handling this huge amount of data creates problems for everyone who wants to do some sort of analysis with that data.
So, If you’ve ever struggled with handling huge amounts of data, and your machine running out of memory, then you’ll love the concept of Iterators in Python.
Therefore, Rather than putting all the data in the memory in one step, it would be better if we could work with it in bits or some small chunks, dealing with only that data that is required at that moment, right? As a result, this would reduce the load on our computer memory tremendously. And this is what exactly the iterators do!
Therefore, you can use Iterators to save a ton of memory, as Iterators don’t compute their items when they are generated, but only when they are called upon.
The first thing we have to note is that “Iterators are Iterables”, and if we use the iterator in a for loop, then it will still work.
number_iterator = iter([1, 2, 3, 4, 5]) for element in number_iterator: print(element)
Output:
1 2 3 4 5
However, the opposite is not always true. For example, iterables like strings and lists are not iterators such that they don’t have the next() method.
'Chirag'.__next__()
Output:
[1, 2, 3, 4, 5].__next__()
Output:
Given the above differences between iterables and iterators, they’re used differently in for loops.
Now, as shown in the below example, we use the same iterable (i.e., a list) in two for loops without getting any errors. By contrast, the iterator can be used just once, as completing the first for loop has already made the iterator iterate all elements such that no more elements to be iterated.
# Use iterables in for-loops for multiple times number_iterable = [1, 2, 3] for i in number_iterable: print(i) print('-----') for i in number_iterable: print(i) print('-----') # Use iterators in for-loops for multiple times number_iterator = iter([1, 2, 3]) for i in number_iterator: print(i) print('-----') for i in number_iterator: print(i) # nothing is printed
Output:
1 2 3 ----- 1 2 3 ----- 1 2 3 -----
Now, in this section we will discuss the difference between Iterables and Iterators:
Let’s understand each point described above with the help of examples:
Let’s understand the above concept with the help of the following example:
list_1 = [1,2,3,4,5] list_2 = iter(list_1) print (list_2) # Iterating through iterable(list_1) using for loop. for element in list_1: print (element,end=" ") print (" ") # Iterating through iterator(list_2) using for loop. for element in list_2: print (element,end=" ")
Output:
1 2 3 4 5 1 2 3 4 5
Let’s understand the above concept with the help of the following examples:
Iterable:
Iterable supports only iter() function(Image Source: Author)
Iterator:
Iterator supports iter() and next() function. (Image Source: Author)
We can get an iterator from an iterable by calling iter() function. Similarly, we can call iter() function on the iterator itself. Then, it will return the iterator object itself.
Let’s understand the above concept with the help of the following examples:
list_1 = [1,2,3,4,5] # Returns an iterator list_2 = iter(list_1) print (list_2) # Calling iter() function on iterator itself. list_3 = iter(list_2) print (list_3) print (list_2 == list_3)
Output:
True
Image Source: Link
Now, let’s see the limits of the iterators:
You can also check my previous blog posts.
Previous Data Science Blog posts.
Here is my Linkedin profile in case you want to connect with me. I’ll be happy to be connected with you.
For any queries, you can mail me on Gmail.
Thanks for reading!
I hope that you have enjoyed the article. If you like it, share it with your friends also. Something not mentioned or want to share your thoughts? Feel free to comment below And I’ll get back to you. 😉
Best explanation available on the internet. Thank you.
Sandhya Singh, who considers women’s development as the cornerstone of the country, is a social worker, who is always ready for the development of people, region and nation.