Home >>Python Tutorial >The Python Iterators

The Python Iterators

Python Iterators

This is the iteration in Python module of the entire Python tutorial. As we have mentioned in this Python tutorial previously, that an iterator is basically a type of object that has a countable number of values situated within it. So, this basically means that an iterator is an object that can be iterated upon which in turn means that you will have the power to transverse through all the values present. Also, in technical terms, an iterator can basically be understood as a type of object which has the power to implement the iterator protocol. And this protocol further consists of two basic methods and those methods are the _iter_ () and the next () method.  

The Iterable Vs. the Iterator

It is important for you to know that all kinds of lists, tuples, sets, and dictionaries are Iterable objects. These objects are just a type of Iterable containers from where you can choose to get an iterator. This means that all of these objects have a type of iter ( ) method. This method is used to get an iterator. If you wish to get an iterator from a particular tuple and then you want to print each of those values then the Python iterator example for this is mentioned below  
mytuple = (“apple”, “banana”, “cherry”)

myit = iter(mytuple)



print (next (myit) )

print (next (myit) )

print (next (myit) )
  It is also important for you to know that all kinds of strings are also iterable objects and you can use these objects to return an iterator. The iteration in Python example for this is mentioned below.
mystr = “banana”

myit = iter (mystr)



print (next ( myit ) )

print (next ( myit ) )

print (next ( myit ) )

print (next ( myit ) )

print (next ( myit ) )

print (next ( myit ) )
 

To Loop Through an Iterator

If you wish to iterate through an iterable object then you can choose to do that by using the for loop method. If you wish to iterate the values of a particular tuple then the Python iterator example for this is mentioned below.
mytuple = (“apple”, “banana”, “cherry”)



for x in mytuple :

   print ( x )
If you instead wish to iterate the characters of a string then you can also do that in the method mentioned below.
mystr = “banana”



for x in mystr :

   print ( x )
In the for loop method, it actually creates a Python list iterator object which then just executes the next () method for the entire loop.  

To Create an Iterator

As we have mentioned above, if you wish to create a particular object or a class as an iterator then you can do that with the help of the _iter_ and the _next_ method. And we have also learned this in the class and objects module that every class has a _init_ function. And this function basically helps you in initializing when the object is being created. So, here the _iter_ method also acts similarly. This means that you can do the operations but you should always remember to return the iterator object itself. The _next_ is also similar because it allows you to do the operations. But you must remember to return the next item that is present in the sequence. If you wish to create a particular iterator that returns number which starts from 1 and the sequence is increased by 1 throughout then the Python next example for this is mentioned below.  
class MyNumbers:

   def _iter_(self) :

    self.a = 1

    return self

def _next_ (self) :

   x = self.a

   self.a += 1

   return x
 

myclass = MyNumbers ( )

myiter = iter ( myclass )

print ( next (myiter) )

print ( next (myiter) )

print ( next (myiter) )

print ( next (myiter) )

print ( next (myiter) )
 

The StopIteration

If you provide the above example with enough of those next ( ) statements then that example would continue forever. That example would also continue forever if you use the for loop method. However, if you wish to prevent any particular iteration from going on forever then you can do that by using the StopIteration statement. You can also choose to add a particular terminating condition to raise a particular error if the iteration has run a specified number of times in the _next_ method. If you want a particular iteration to stop after 20 then the Python _iter_ example for this is mentioned below.
class MyNumbers :

def _iter_ (self) :

   self.a = 1

   return self

def _next_ (self) :

  if self.a <= 20 :

   x = self.a

   self.a += 1

   return x

else :

      raise StopIteration



myclass = MyNumbers ( )

myiter = iter ( myclass )



for x in myiter :

   print ( x )
With this, we finish the iteration in Python part of our entire Python tutorial.

No Sidebar ads