python tutorial - Iterator Python | Iterators in Python - learn python - python programming

 python iterator

Learn Python - Python tutorial - python iterator - Python examples - Python programs

Iterator in python is any python type that can be used with a ‘for in loop’. Python lists, tuples, dicts and sets are all examples of inbuilt iterators. These types are iterators because they implement following methods. In fact, any object that wants to be an iterator must implement following methods.

iterable objects examples

  1. __iter__ method that is called on initialization of an iterator. This should return an object that has a next or __next__ (in Python 3) method.
  2. next ( __next__ in Python 3) The iterator next method should return the next value for the iterable. When an iterator is used with a ‘for in’ loop, the for loop implicitly calls next() on the iterator object. This method should raise a StopIteration to signal the end of the iteration.
iter function example

Below is a simple Python program that creates iterator type that iterates from 10 to given limit. For example, if limit is 15, then it prints 10 11 12 13 14 15. And if limit is 5, then it prints nothing.

implementing iterator as a class python

python - Sample - python code :

# A simple Python program to demonstrate
# working of iterators using an example type
# that iterates from 10 to given value
# An iterable user defined type
class Test:
    # Cosntructor
    def __init__(self, limit):
        self.limit = limit
    # Called when iteration is initialized
    def __iter__(self):
        self.x = 10
        return self
    # To move to next element. In Python 3,
    # we should replace next with __next__
    def next(self):
        # Store current value ofx
        x = self.x
        # Stop iteration if limit is reached
        if x > self.limit:
            raise StopIteration
        # Else increment and return old value
        self.x = x + 1;
        return x
# Prints numbers from 10 to 15
for i in Test(15):
# Prints nothing
for i in Test(5):

python tutorial - Output :


Examples of inbuilt iterator types.

python implementing iterator as a class

python - Sample - python code :

# Sample built-in iterators
# Iterating over a list
print("List Iteration")
l = ["wikitechy", "best e-learning", "website"]
for i in l:
# Iterating over a tuple (immutable)
print("\nTuple Iteration")
t = ("wikitechy", "best e-learning", "website")
for i in t:
# Iterating over a String
print("\nString Iteration")    
s = "Wikitechy"
for i in s :
# Iterating over dictionary
print("\nDictionary Iteration")   
d = dict() 
d['xyz'] = 123
d['abc'] = 345
for i in d :

python tutorial - Output :

List Iteration
best e-learning

Tuple Iteration
best e-learning

String Iteration

Dictionary Iteration
xyz  123
abc  345

Wikitechy tutorial site provides you all the learn python , python programming help ,online python learning , learn to program with python , online python training free

Related Searches to Iterators in Python

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add to your ad blocking whitelist or disable your adblocking software.