Iterators and Iterables

So we have learned about lists, and we also have seen the concept of functions. Let’s make a function which will print the squares of all elements of the given list. What we have done earlier is take an empty list, check for each element present in my given list, square each element and append the squared value in the empty list. Finally, print the empty list. In function, we have to put the above logic inside def function and instead of printing the result of an empty string, we will return this result back to the def function and will print the function. Let’s code it now.

So we got a list of squared values of the given list. Let’s deep dive into this small already done program. What is this for loop doing? It’s checking for the first value, then the second, then the third value and so on till all values are fetched. In the language of mathematics, it’s performing iterations, and thus it’s an iterator.

So,  iterators are used to fetch one value at a time.  Other than using this for loop, there are two more ways to fetch one value at one time. The first method is using index position. We can write a[0] and it will give the first value, a[1] will give a second value and so on. But this indexing method won’t work on unordered data structures, such as sets and dictionaries.

Another method is to use a function called iter (). It’s written as the double underscore iter followed by double underscore. These double underscore methods are also called the magic methods. This double underscore iter method helps us to find out if something is iterable or not. We know that lists are iterable. Anything on which the loop can run are iterables. When we write for I in list, it means that for loop is trying to loop over our list. String, dictionaries, sets, tuples all are iterables because we can use loops to fetch data from them. Let’s make a list and by using dir () we will check if it has __iter__ method or not. If this method exists then that thing becomes iterable.

As you can see the marked red circle, it has the iter method.

It means that lists are iterable because it has this method. But is it an iterator? Can we use a list to do the function of a for loop? Iterator has a state through which it knows where it is during an iteration and by using double underscore next method they get their next value. If we see here in the output then there is no next method. Hence, a list has no state to check the next value and that’s why a list is not an iterator. 

So the list has no attribute next. This can be verified from the previous output as well. There you can see the iter () method but not the next() method. That’s why lists are not iterators although they are iterables. There is another way to write these magic methods. Usually they are written as functions. Let’s pass my list name into this iter function.

Similarly, we can use the next () function.

OOPS!! An error occurred. If the list has become an iterator then it should have the next() attribute also so that it can iterate the next value. Let me first pass this into another variable and then print it. 

Now it’s working correctly. You can see that both __iter__ and __next__ method are present.

This next () method preserves the state of the last value. So what the for loop does is that it calls iter on our object and returns an iterator that we can loop over. At the back end it calls for the next function again and again and generates all the values. The iterators use both the iter and the next methods continuously at its back end to fetch next values and then print the complete result. Conceptually, now you know why for loops or any other loops are iterators, how they iterate and why everything cannot be an iterator? This knowledge is also helpful in making our own iterator functions using the iter function. But there is another method through which you can make iterators without using the iter.

Design a site like this with WordPress.com
Get started