Python Iterator with Syntax

Python Iterator with Syntax

Introduction to Python Iterators

An iterator in Python programming language is an object which you can iterate upon. That is, it returns one object at a time. Iterators are implicitly implemented in constructs like for-loops, comprehensions, and python generators. The iter() and next() functions collectively form the iterator protocol.

If we can get iterable from an object in python, it is iterable. Examples include python lists, python tuples, and python strings. Let’s revise Python Syntax and Python Method.

Building an Iterator in Python

To build a python iterator, we use the iter() and next() functions. Let’s begin with iter() to create an iterator.

First, we create a list evens to store even numbers from 2 to 10.

  1. >>> evens=[2,4,6,8,10]

Then, we apply the iter() function to this list to create an iterator object. We store it in the variable evenIterator.

  1. >>> evenIterator=iter(evens)
  2. >>> evenIterator

<list_iterator object at 0x05E35410>

Steps to Install Python on Windows

Remember, it does not have to be a list you create an iterator on.

  1. >>> iter((1,3,2))

<tuple_iterator object at 0x05E35550>

Now, to access the first element, we apply the function next() on the iterator object.

  1. >>> next(evenIterator)

2

We do the same for the next element(s) as well.

  1. >>> next(evenIterator)

4

  1. >>> next(evenIterator)

6

  1. >>> next(evenIterator)

8

  1. >>> next(evenIterator)

10

We have reached the end of the list. When we call it once more, we raise a StopIteration error (exception). The interpreter internally catches it.

  1. >>> next(evenIterator)
  2. Traceback (most recent call last):
  3. File "<pyshell#442>", line 1, in <module>
  4. next(evenIterator)

StopIteration

  • __next__()

Let us see the tremendous Python career opportunities in 2018

You can also traverse the iterator using the __next__() method.

  1. >>> nums=[1,2,3]
  2. >>> numIter=iter(nums)
  3. >>> numIter.__next__()

1

  1. >>> next(numIter)

2

  1. >>> numIter.__next__()

3

  1. >>> numIter.__next__()
  2. Traceback (most recent call last):
  3. File "<pyshell#448>", line 1, in <module>
  4. numIter.__next__()

StopIteration

We can see this with the dir() function we saw in in-built functions in Python.

  1. >>> dir(numIter)

[‘__class__’, ‘__delattr__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__init_subclass__’, ‘__iter__’, ‘__le__’, ‘__length_hint__’, ‘__lt__’, ‘__ne__’, ‘__new__’, ‘__next__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__setattr__’, ‘__setstate__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’]

4. For-loop in Python Iterator

You can also use a for loop in python to iterate on an iterable like a python list or a python tuple.

  1. >>> for i in 'Python':
  2. print(i)

P

y

t

h

o

n

But how is this actually implemented? Let’s take a look.

  1. >>> iter_obj=iter('Python')
  2. >>> while True:
  3. try:
  4. i=next(iter_obj)
  5. print(i)
  6. except StopIteration:
  7. break

P

y

t

h

o

n

So, this is how the above for loop is actually implemented.

Top python Books to learn Python programming language.

Read Complete Article>>

To view or add a comment, sign in

More articles by Malini Shukla

Others also viewed

Explore content categories