Beginner Introduction to Python Iterator

In this article you will learn about Python iterator.

Python Iterator – Before moving ahead, let’s know a little bit about Python for loop

Iterators – It contains a countable number of elements. An iterator is an object which can go through all elements, means – looping through all elements.

In Python, an iterator is an object, which has two methods __iter__() and __next__() .

Iterators – Python contains some iterable objects like Lists, tuples, dictionaries, and sets. It’s all iterable elements and can get by iterating.

Python’s all these objects includes iter() method which is used to get an iterator.

Example 1- Use iter() method to get iterator.

x = ['A', 'B', 'C', 'D']
z = iter(x)
 
print(next(z))
print(next(z))
print(next(z)) 
print(next(z)) 
python-iterator

Explanation – In the above example, elements of the list are iterated through each element with iter() function and returned all elements one by one as the next() function is used to get next element from iterator.

Like all iterable objects, string is also iterator and can return an iterator.

Example 2- Use iter() method to get iterator.

x = ['Rewind']
z = iter(x)

print(next(z))
python-iterator

Explanation – In the above example, as list contained only one element therefore, iterator retuned only one element.

Looping Through an Iterator – An iterable object can be looped through using for loop.

Example 3- Use of for loop to Iterate over elements.

z = ['A', 'B', 'C', 'D']

for x in z:
    print(x)
python-iterator

Explanation – In the above example, elements of the list are iterated through each element with for loop and returned all elements from iterator.

Example 4- Use of loop to iterate over the string.

z = 'Rewind'

for x in z:
    print(x)
python-iterator

Explanation – In the above example, elements of the list iterated through each element with for loop and returned all elements from iterator.

To create an object, implement the __iter__() and __next__methods () to object.

The __iter__() method is similar as __init__ method, used to initialized object’s property, but always return the iterator object itself.

Example 5- Create an iterator return a sequence of numbers and will increase sequence one by one.

class Mynum:

     def __iter__(self):
         self.a = 1
         return self

     def __next__(self):
         x = self.a
         self.a += 1
         return x

 z = Mynum()
 v = iter(z)

 print(next(v))
 print(next(v)) 
python-iterator

Explanation – In the above example, a class is defined name Mynum that held two def functions inside itself. Here def function is used to get the number and adding the number to the current number. As function called, next() function get the latest number and print() function execute it.

StopIteration – To stop the iteration or to stop execute given statement use StopIteration.

Example 6- Use StopIteration statement to stop sequence of numbers at particular position.

class Mynum:

     def __iter__(self):
         self.a = 1
         return self

     def __next__(self):
         if self.a < 5 :
             x = self.a
             self.a += 1
             return x
         else:
             raise StopIteration

 z = Mynum()
 v = iter(z)

 for n in v:
     print(n) 
python-iterator

Output –

python-iterator

Explanation – In the above example, as function is called, elements iterate one by one until condition gets true. As condition goes False, it StopIteration according to else condition.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment down below.

Like us on

Leave a Comment

Stay in the loop

codingstreets