Table of contents
1.
Introduction
2.
Iterators in Python
3.
Difference Between Iterator and Iterable
4.
Example
5.
Python iter() Example
6.
Creating an Iterator
6.1.
Example: Creating a Number Iterator
7.
StopIteration Exception
7.1.
Example
7.2.
Key Points
8.
Practical Uses of Iterators
9.
Frequently Asked Questions
9.1.
What is the difference between iter() and next()?
9.2.
Why use custom iterators?
9.3.
What happens if StopIteration is not handled?
10.
Conclusion
Last Updated: Aug 21, 2025
Easy

Python Iterator

Author Gaurav Gandhi
0 upvote

Introduction

An iterator in Python is an object that contains a sequence of values, allowing you to traverse through them one by one. It is an object that can be iterated upon, meaning you can access each value in the sequence in a controlled manner. Iterators are widely used in Python for looping over data structures like lists, tuples, and more.

Python Iterator

In this article, you’ll learn what iterators are, how they differ from iterables, and how to create your own iterator in Python. 

Iterators in Python

An iterator in Python is an object that allows iteration over a sequence of data. It represents a flow of data and defines two essential methods:

  • __iter__() – This returns the iterator object itself.
     
  • __next__() – This retrieves the next element in the sequence.
     

Here’s a simple example to understand iterators:

# Example of an iterator

# Example of an iterator

class MyIterator:
    def __init__(self, start, end):
        self.current = start
        self.end = end

    def __iter__(self):
        return self

    def __next__(self):
        if self.current >= self.end:
            raise StopIteration
        else:
            self.current += 1
            return self.current - 1

# Creating an iterator instance
my_iter = MyIterator(1, 5)

# Iterating over the iterator
for num in my_iter:
    print(num)
You can also try this code with Online Python Compiler
Run Code

 

 Output: 

1
2
3
4


When the next() function reaches the end of the sequence, it raises a StopIteration exception. This behavior ensures that the iteration stops gracefully.

Difference Between Iterator and Iterable

Understanding the difference between iterators and iterables is essential:

AttributeIteratorsIterables
DefinitionAn object that implements the iterator protocol (iter() & next()).An object that can be iterated over, either with a for loop or by calling iter().
IterationCan be iterated over only once. Once exhausted, they cannot be reset or reused.Can be iterated over multiple times, creating a new iterator each time.
MethodsHas a next() method that returns the next item in the sequence. Raises StopIteration when exhausted.Has an iter() method that returns a fresh iterator object.
ExamplesFile objects, generators.Lists, strings, dictionaries, sets, tuples.

This table highlights the main differences and usage of iterators and iterables in Python.

Example

# Iterable
my_list = [1, 2, 3]

# Iterator
iterator = iter(my_list)

print(next(iterator))  # Works, output: 1
print(next(my_list))   # Error: 'list' object is not an iterator
You can also try this code with Online Python Compiler
Run Code


Explanation:
my_list is an iterable because it can be converted into an iterator using iter(). However, it is not an iterator itself because it lacks the __next__() method.

Python iter() Example

The iter() function is used to convert an iterable into an iterator. Let’s see an example:

# Using iter() on a string
my_string = "Hello"
string_iterator = iter(my_string)
print(next(string_iterator)) 
print(next(string_iterator))  
print(next(string_iterator)) 
You can also try this code with Online Python Compiler
Run Code

 

Output

H
e
I


Explanation:

  1. The string my_string is an iterable.
     
  2. The iter() function converts it into an iterator.
     
  3. Using next(), we can retrieve each character one by one.
     

This functionality is particularly useful in loops, where iteration happens automatically. For instance:

# Using iter() in a loop
my_list = [10, 20, 30]
for item in iter(my_list):
    print(item)
You can also try this code with Online Python Compiler
Run Code


Output:

10
20
30

Creating an Iterator

In Python, you can create an iterator by defining a class that implements two key methods: __iter__() and __next__(). These methods allow the object to be iterated over, enabling it to produce a sequence of values one at a time.

__iter__() Method: This method returns the iterator object itself. It is necessary because Python expects the object to be iterable.
 

__next__() Method: This method is used to get the next item in the sequence. Once all items have been accessed, it should raise the StopIteration exception to signal the end of the sequence.

Example: Creating a Number Iterator

class MyIterator:
    def __init__(self, start, end):
        self.current = start
        self.end = end

    def __iter__(self):
        return self

    def __next__(self):
        if self.current > self.end:
            raise StopIteration
        else:
            self.current += 1
            return self.current - 1

# Using the iterator
my_iter = MyIterator(1, 5)

for num in my_iter:
    print(num)
You can also try this code with Online Python Compiler
Run Code


Output:

1
2
3
4
5


Explanation:

  • The iterator starts at 1 and goes up to 5 (inclusive).
     
  • The __next__() method increases the current value by 1 each time it's called and returns the previous value.
     
  • Once the current value exceeds 5, the StopIteration exception is raised, stopping the loop.
     

Thus, the numbers 1, 2, 3, 4, and 5 are printed during the loop.

StopIteration Exception

In Python, the StopIteration exception is used to signal the end of an iteration. When you are using an iterator, the __next__() method is called to get the next item in a sequence. Once all items have been retrieved, Python raises the StopIteration exception to let you know there are no more items to process.

This is important because it prevents errors when trying to access items that do not exist. For example, when using a loop to iterate over a list, the loop will stop automatically when it reaches the end, thanks to the StopIteration exception.

Example

my_list = [1, 2, 3]
iterator = iter(my_list)
try:
    print(next(iterator))  
    print(next(iterator))  
    print(next(iterator)) 
    print(next(iterator)) 
except StopIteration:
    print("Iteration is complete.")
You can also try this code with Online Python Compiler
Run Code

 

Output

1
2
3

Key Points

  • StopIteration is automatically handled in a for loop, making it a convenient tool for iteration.
     
  • If you manually handle iterators, you need to account for this exception to prevent errors.

Practical Uses of Iterators

Reading Files:
Iterators are used to read large files line by line, avoiding memory overload.

with open('example.txt', 'r') as file:
    for line in iter(file.readline, ''):
        print(line.strip())


Infinite Streams:
Iterators can create infinite data streams, such as generating Fibonacci numbers.

Frequently Asked Questions

What is the difference between iter() and next()?

  • iter() converts an iterable into an iterator.
     
  • next() retrieves the next item from an iterator.

Why use custom iterators?

Custom iterators allow you to define unique ways to traverse data, such as generating sequences or processing elements dynamically.

What happens if StopIteration is not handled?

If StopIteration is not handled in a manual iteration, your program will throw an error. However, it is automatically managed in loops like for.

Conclusion

In this article, we discussed the concept of iterators in Python, their relationship with iterables, and how to use the iter() function. You also learned how to create custom iterators and handle the StopIteration exception. By mastering iterators, you can work efficiently with data streams and create more powerful, memory-efficient Python programs.

Recommended Readings:

Live masterclass