Malhar Lathkar Malhar Lathkar
Updated date May 31, 2021
An Iterator is an important component of Python’s data model. Python’s container data types (list, tuple, string, etc.) use an iterator. Iterator represents a stream of items, providing one item at a time.

An Iterator is an important component of Python’s data model. Python’s container data types (list, tuple, string, etc.) use iterator under the hood while traversing through the collection. The most common way of element-wise traversal of a collection is by Python’s for a statement as below:

>>> num = [11,22,33,44]

>>> for x in num:

      print (x)

Output:

11

22

33

44

However, execution of for loop internally uses an iterator object. Iterator represents a stream of items, providing one item at a time whenever __next__() method is called. Python’s built-in function iter(), which in turn calls __iter__() method inherited from Object class, returns an iterator from any sequence type object.

>>> num = [11,22,33,44]

>>> numit=iter(num)

>>> type(numit)

<class 'list_iterator'>

>>> #is equivalent to

>>> numit=num.__iter__()

>>> type(numit)

<class 'list_iterator'>

The iterator class has __next__() method. It returns the next available item in the pipeline.

>>> numit.__next__()

11

>>> numit.__next__()

22

Built-in next() function internally implements __next__() method.

>>> next(numit)

33

>>> next(numit)

44

Call to next() after the collection is exhausted, the Iterator raises StopIteration exception.

>>> next(numit)

Traceback (most recent call last):

  File "<pyshell#13>", line 1, in <module>

    next(numit)

StopIteration

The above list of numbers can be equivalently traversed by using Iterator object and next() function as follows:

>>> numit=iter(num)

>>> while True:

      try:

         x=next(numit)

         print (x)

      except StopIteration:

         break

Output:

11

22

33

44

When a for loop traverses the list, it actually builds an iterator implicitly and calls next() for successive iteration till it exhausts and doesn’t need to explicitly catch StopItrration.

>>> num = [11,22,33,44]

>>> numit=iter(num)

>>> for x in numit:

      print (x)

Output:

11

22

33

44

The irer() function returns iterator from any sequence type object (list, tuple, string). That’s why they are called iterables.

>>> num = [11,22,33,44]

>>> iter(num)

<list_iterator object at 0x000001F9F854AA88>

>>> num = (11,22,33,44)

>>> iter(num)

<tuple_iterator object at 0x000001F9F8565C88>

>>> name = 'TechieClues'

>>> iter(name)

<str_iterator object at 0x000001F9F855F988>

>>> num ={11,22,33,44}

>>> iter(num)

<set_iterator object at 0x000001F9F85A8B88>

Even though Python’s Dictionary object is not a sequence, it also returns an iterator of its keys.

>>> num = {'one':1, 'two':2, 'three':3, 'four':4}

>>> it = iter(num)

>>> type(it)

<class 'dict_keyiterator'>

>>> for x in it:

      print (x)

Output:

one

two

three

four

Python’s iterator protocol requires that an iterator class must implement __iter__() and __next__() methods. We shall write a custom iterator class that follows these requirements:

class myiterator:

    def __init__(self,x):

        self.pos = -1

        self.num=x

    def __iter__(self):

        return self

    def __next__(self):

        self.pos=self.pos+1

        if self.pos>=len(self.num):

            raise StopIteration

        return self.num[self.pos]

We can now obtain an iterator object from a list and traverse it as done earlier.

myit=myiterator([11,22,33,44])

while True:

    try:

        x=myit.__next__()

        print (x)

    except StopIteration:

        break

The built-in iter() function has another signature as below:

iter(callable, sentinel)

The first parameter to the function is a callable (a function or class with __call__() method), whereas sentinel, the second parameter refers to the last item in the iterator upon reaching which, StopIteration is raised.

count=0

def counter():

    global count

    count=count+1

    return (count)


countit = iter(counter, 5)

for x in countit:

    print (x)

The above code builds an iterator of numbers 1 to 4. The subsequent for loop traverses through the items.

Lastly, we shall have a look at Python’s File object as an iterator. As mentioned earlier, any stream object is in fact an iterator. We can read a file by perpetually calling next() function on a File object as follows:

file = open("test.txt","r")

while True:

    try:

        line = next(file)

        print (line)

    except StopIteration:

        break

This was a brief discussion on Iterators in python.

Comments (0)

There are no comments. Be the first to comment!!!