After reading this Python Iterators topic, you will know how to implement an iterator and use a loop over an iterator in Python, you will understand iterator two methods __iter()__ and __next()__, & stopiteration statement in Python.
In python, an iterator is an object and iterator object is generally associated with two methods as __iter()__ and __next()__. Lists, tuples, dictionaries, sets, etc. all are examples of iterable objects. The object said to be iterable when it gives iterator and for doing this the object used the method __iter()__. The __next()__ method is used to visit each item of lists, tuples, dictionaries, sets, etc. in python.
Examples:
Program (1): To show how to return an iterator from a tuple and print each item in Python.
# storing items 2,3 and 4 in the tuple x = (2, 3, 4) # using the method iter() y = iter(x) # print each item of tuple using method next() print(next(y)) print(next(y)) print(next(y))
Output(1)
2 3 4
Program (2): To show how to return an iterator from string and print each character in Python.
# storing the string x = "python" # using the method iter() y = iter(x) # print each characer of string using method next() print(next(y)) print(next(y)) print(next(y)) print(next(y)) print(next(y)) print(next(y))
Output(2)
p y t h o n
Using loop over the iterator
Using loop over the iterator means we move through an iterable object and visit each item of it.
Examples:
Program (1): To show how to iterate the characters of a string and print each character in Python.
# storing the string x = "python" # use for loop to iterate through an iterable object as string for i in x: print(i)
Output(1)
p y t h o n
Program (2): To show how to iterate the items of a tuple and print each item in Python.
# storing items 2,3 and 4 in the tuple x = (2,3,4) # use for loop to iterate through an iterable object as tuple for i in x: print(i)
Output(2)
2 3 4
How to build an iterator in Python
For building iterator as an object or class in python use two methods are given as __iter__() and __next__().
The object said to be iterable when it gives iterator and for doing this the object used the method __iter()__.
The __next()__ method is used to visit and return each item in the sequence.
Example:
Program (1): To create an iterator that returns numbers in decreasing order as 5, 4, 3, 2 and 1.
Consider the statements in python is given by
class dec_numbers: def __iter__(self): self.a = 5 return self def __next__(self): x = self.a self.a -= 1 return x my_classname = dec_numbers() # using the method iter() start_iter = iter(my_classname) print(next(start_iter)) print(next(start_iter)) print(next(start_iter)) print(next(start_iter)) print(next(start_iter))
Output(1)
5 4 3 2 1
StopIteration statement in Python
We already discussed next() method in the topic, how to build an iterator in python. The StopIteration statement used to run next() method for a particular predefined number of iterations.
Example:
Program (1): To create an iterator that returns numbers in decreasing order as 10, 9, 8, 7, 6, 5
class dec_numbers: def __iter__(self): self.a = 10 return self def __next__(self): #to stop itterations as number 5 print if self.a >= 5: x = self.a self.a -= 1 return x else: raise StopIteration my_classname = dec_numbers() # using the method iter() start_iter = iter(my_classname) for x in start_iter: print(x)
Output(1)
10 9 8 7 6 5