Join our newsletter for the latest updates.

Python iter()

The Python iter() function returns an iterator for the given object.

The iter() function creates an object which can be iterated one element at a time.

These objects are useful when coupled with loops like for loop, while loop.

The syntax of the iter() function is:

iter(object, sentinel)

iter() Parameters

The iter() function takes two parameters:

  • object - object whose iterator has to be created (can be sets, tuples, etc.)
  • sentinel (optional) - special value that is used to represent the end of a sequence

Return value from iter()

  • The iter() function returns an iterator object for the given object.
  • If the user-defined object doesn't implement __iter__(), and __next__() or __getitem()__, the TypeError exception is raised.
  • If the sentinel parameter is also provided, iter() returns an iterator until the sentinel character isn't found.

Example 1: Working of Python iter()

# list of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
vowels_iter = iter(vowels)

print(next(vowels_iter))    # 'a'
print(next(vowels_iter))    # 'e'
print(next(vowels_iter))    # 'i'
print(next(vowels_iter))    # 'o'
print(next(vowels_iter))    # 'u'



Example 2: iter() for custom objects

class PrintNumber:
    def __init__(self, max):
        self.max = max

    def __iter__(self):
        self.num = 0
        return self

    def __next__(self):
        if(self.num >= self.max):
            raise StopIteration
        self.num += 1
        return self.num

print_num = PrintNumber(3)

print_num_iter = iter(print_num)
print(next(print_num_iter))  # 1
print(next(print_num_iter))  # 2
print(next(print_num_iter))  # 3

# raises StopIteration


Traceback (most recent call last):
  File "", line 23, in 
File "", line 11, in __next__

Example 3: iter() with sentinel parameter

with open('mydata.txt') as fp:
    for line in iter(fp.readline, ''):

When you run the program, it will open the mydata.txt file in reading mode.

Then, the iter(fp.readline, '') in the for loop calls readline (which reads each line in the text file) until the sentinel character, '' (empty string), is reached.

Recommended Reading: Python Iterators