Iterators in Python programming language are widely used everywhere. They are implemented within the loops, comprehension, generators but are hidden in normal sight. This Iterator in python is simply an object which will return the data one element at a time. Here the python iterator object must be implemented to special methods,_iter_() and _next_(), it is called as iterator protocol. An object is called iterable which is called an iterator form. There are many built-in containers in python like, list, tuple, string are iterables. Here the iter() function is going to return on iterator from them.
Iterable will be considered as an object which can be iterated. It always generates an iterator when it is passed to the iter()method. Lists, tuples, strings, dictionaries, and sets are all iterable objects. They are iterable containers that we may pass us to convert into an iterator. See to it that iterator will also be iterable but it is not an iterator. An iterator that will be created from the iterable by making use of the function iter(). When we pass this tuple to an iter() function we will get an iterator. This will be possible by the class object which has method _iter_() which will return an iterator.
The method _iter_() method will return the iterator object itself. This needs to allow both containers and iterators to be used with for and in the statements.This method corresponds to the tp_iter slot of the type structure for the python object in the python/C API.
_next_() method will return the next item from the container. If we have no further items, raise the stop iteration exception. This method corresponds to the tp_iternext slot of the type structure for the python objects in the python/C API.
Iterating through an iterator:
We mainly use the next() function where the manually iteration through all the items of an iterator. When we reach the end and there will be no more data returned it will be raise the stopiteration Exception the example is
#define a list
my_list=[4,7,0,3]
#get an iterator using iter()
my_iter=iter(my_list)
#iterate through it using next()
#output: 4
print(next(my_iter))
#next(objj) is same as objj._next_()
#output:0
print(my_iter._next_())
#output:3
print(my_iter._next_())
#this will give an error,no items left
next(my_iter)
The output
4
7
0
3
Traceback(most current call left):
File”<string>”,line 24, in<module>
next(my_iter)
stopiteration
Iterating an iterator using the loops:
When we are using the next() function repeatedly why shouldn’t we use the while loop and make the work easier, we also will use the exception handling for the error that we get when the iterator has no elements left. So whenever the “stopiteration” occurs the control will go except the block and break statement will be executed.
# define an iterable such as a list
list1=[0,1,2,3,4,5,6,7,8,9,]
# get an iterator using iter()
iter1=iter(list1)
# infinite loop
while True:
try:
# get the next item
print(next(iter1))
# do something with element
except StopIteration:
# if StopIteration is raised, break from loop
break
Output:
0
1
2
3
4
5
6
7
8
9
Iterables
An iterable is considered a python object that has the capability of getting back its members one at a time by allowing it to be iterated over in a for loop. There are similar examples that include lists, tuples, and also strings that will be in a sequence that is iterated over in a for loop. We will always encounter the important non-sequential collections, like dictionaries and sets where these iterables as well. It will be possible to have an iterable that generates each one which of its members in any memory at once.
The Functions of Iterables:
We have some built-in functions which will accept iterables as an argument like list, tuple, dict, set that will construct a list tuple dictionary or maybe set respectively from the contents of an iterable.
Sum: Which adds the data of an iterable.
Sorted: Which returns back the list of the sorted data of an iterable.
Questions
- Explain the python iterators with an example?
- What are Iterables give an example?