Iterators in Python | Python Programming
Note: In built iterator available in Python is for loop.
Iterators: Iterators in python is nothing, but it is just an object which contains a countable number of finite or some time infinite number of values. An iterator object must implement two magic method or special method __iter__ and __next__ .
Both methods are very important to create a complete iterable object. So, that’s why both these methods ( __iter__ and __next__) collectively called iterator protocol. (__iter__ and iter() ALSO __next__ and next()) are same.
Important: If you don’t know about magic methods then please have a look first.
The iter() function or __iter__ returns one value at a time. Just after iter() function finishes its execution we implement next() function which helps in getting the next value.
#Example iterator in python num = [1,2,3,4,5] iterator = iter(num) print(iterator)
#<list_iterator object at 0x7f4b32b42eb8>
Explanation: Here we have defined one list “num” and created an iterator object using iter() function. It returned us the memory address where object is stored. But here, we want to access the iterator element. For that we need to use both iter() and next() method together. Let’s take one more example to understand it better.
#Magic_method__len__()_example num = [1,2,3,4,5] iterator = iter(num) print(iterator.__next__()) # print(next(iterator))
Explanation: By using next() function we successfully got the first element. But the problem is, it will only returns the first element. To get the next element, we have to use next() function again and again.
#Magic_method__len__()_example num = [1,2,3,4,5] iterator = iter(num) print(iterator.__next__()) # print(next(iterator)) print(iterator.__next__()) print(iterator.__next__())
#Output 1 2 3
Hence, both functions iter() and next() together, satisfies the iterator protocol. Using the concept of iterator, we can create our own object same as flow control statements (for loop, while loop etc.)
Example 4: In this example, we will create our own object. To create our own object, we need our own class. So, let’s implement accordingly:
# Creating our own object using iterator class Test: def __init__(self): self.num = 1 def __iter__(self): return self def __next__(self): if self.num <= 10: val = self.num self.num += 1 return val else: raise StopIteration values = Test() for i in values: print(i)
# Output 1 2 3 4 5 6 7 8 9 10
Explanation: Let’s discuss each step One by One:
Step 1: We have defined our own class “Test”
#class definition class Test:
Step 2: Inside our Test class , an instance variable num has been initialized with value 1. Instance variable num acting as the starting index of our iteration.
# Initializing the starting index of iterator def __init__(self): self.num = 1
Step 3: Here after constructor, we have defined our iter() function and it is returning the memory address where our object is stored. Actually we are not defined the iter() method, we are overriding that as it has already been defined in python. In magic method concept, it has been cleared already.
#overriding __iter__ method def __iter__(self): return self
Step 4: Here __iter__ method returned us an iterator object. Now we will implement, for getting the object value itself. Inside __next__ method, we are using if control statement along with else: because we want iterate from 1 to 10. So, if loop here check if the value of instance variable num should be smaller than or equal to 10. If it does not satisfy the condition, then loop control jump to else and terminate the loop execution.
Here value of our instance variable num is 1 , hence it will enter to for loop and value of instance variable is initialize to variable val. Now value of num is stored to val and it will be returned and we’ll get the result 1. But here statement
self.num += 1 is incrementing the value of our instance variable to +1. And hence loop will execute till the value of num <= 10 and our function is keep on returning the value stored in variable val.
#__next__ method definition def __next__(self): if self.num <= 10: val = self.num self.num += 1 return val else: return StopIteration
Step 5: Finally we created the object of our class and got the final output:
#Object Creation values = Test() for i in values: print(i)
Note: If you are beginner, Then it is common that you have faced difficulty in understanding this topic. To make it more clear please also read magic methods topic.