For working professionals
For fresh graduates
More
13. Print In Python
15. Python for Loop
19. Break in Python
23. Float in Python
25. List in Python
27. Tuples in Python
29. Set in Python
53. Python Modules
57. Python Packages
59. Class in Python
61. Object in Python
73. JSON Python
79. Python Threading
84. Map in Python
85. Filter in Python
86. Eval in Python
96. Sort in Python
101. Datetime Python
103. 2D Array in Python
104. Abs in Python
105. Advantages of Python
107. Append in Python
110. Assert in Python
113. Bool in Python
115. chr in Python
118. Count in python
119. Counter in Python
121. Datetime in Python
122. Extend in Python
123. F-string in Python
125. Format in Python
131. Index in Python
132. Interface in Python
134. Isalpha in Python
136. Iterator in Python
137. Join in Python
140. Literals in Python
141. Matplotlib
144. Modulus in Python
147. OpenCV Python
149. ord in Python
150. Palindrome in Python
151. Pass in Python
156. Python Arrays
158. Python Frameworks
160. Python IDE
164. Python PIP
165. Python Seaborn
166. Python Slicing
168. Queue in Python
169. Replace in Python
173. Stack in Python
174. scikit-learn
175. Selenium with Python
176. Self in Python
177. Sleep in Python
179. Split in Python
184. Strip in Python
185. Subprocess in Python
186. Substring in Python
195. What is Pygame
197. XOR in Python
198. Yield in Python
199. Zip in Python
Iterator in Python is a powerful programming concept that allows you to access elements of a collection one at a time without using indexing. It is commonly used when working with loops, large datasets, or when memory efficiency is a concern. Python provides built-in support for iterators through special methods and functions, making iteration simple and readable.
In this article, you will explore what an iterator is and how it differs from an iterable. Then, you will go through step-by-step examples ranging from basic to advanced levels. You’ll also learn when to use iterators in real-life situations, how the StopIteration exception works, and how to implement your own iterator using a class.
Pursue our Software Engineering courses to get hands-on experience!
An iterator in Python is an object that enables you to step through a sequence of elements one item at a time. It does not rely on index positions. Instead, it uses a specific set of methods to access each item sequentially.
To qualify as an iterator, an object must follow the iterator protocol. That means it must implement two built-in methods: __iter__() and __next__(). The __iter__() method returns the iterator object itself. The __next__() method returns the next item from the collection. If there are no items left, it raises a StopIteration exception.
You can create an iterator from an iterable (like a list or tuple) using Python’s built-in iter() function. Then, you can fetch elements one by one using the next() function.
Take your skills to the next level with these top programs:
Here’s the syntax:
iterator = iter(iterable_object) # Create an iterator from an iterable
item = next(iterator) # Get the next item from the iterator
Here,
Let’s break down the syntax by creating a simple iterator manually using a custom class. This will help you understand how the iterator protocol works behind the scenes.
# Define a custom iterator class
class CountUpTo:
def __init__(self, max_value):
self.max = max_value # Maximum number to count up to
self.current = 1 # Start from 1
def __iter__(self):
return self # Return the iterator object itself
def __next__(self):
if self.current <= self.max:
num = self.current
self.current += 1 # Move to the next number
return num
else:
raise StopIteration # Stop when the limit is reached
# Create an instance of the iterator
counter = CountUpTo(3)
# Use the iterator manually with next()
print(next(counter)) # Output: 1
print(next(counter)) # Output: 2
print(next(counter)) # Output: 3
Output:
1
2
3
Explanation:
To fully understand how an iterator in Python works, it's helpful to see real examples in action. We will begin with a simple use case, move to a more controlled iteration using exception handling, and finally create a custom iterator with advanced behavior.
# Create a list of students
students= ["Anil", "Kunal", "Harsh"]
# Get the iterator object using iter()
student_iterator = iter(students)
# Access each item using next()
print(next(student_iterator)) # Output: Anil
print(next(student_iterator)) # Output: Kunal
print(next(student_iterator)) # Output: Harsh
Output:
Anil
Kunal
Harsh
Explanation:
Now let’s manually handle the StopIteration exception. This gives us better control, especially when we don’t know the exact size of the data.
# Define a list of numbers
numbers = [10, 20, 30]
# Get the iterator object
num_iter = iter(numbers)
# Use a while loop with exception handling
while True:
try:
# Print the next number
print(next(num_iter))
except StopIteration:
# Break the loop when iteration ends
break
Output:
10
20
30
Explanation:
Must Explore: String split() Method in Python
At the advanced level, you may want to create your own iterator to control how and when values are produced. This is useful when working with large datasets, mathematical sequences, or custom traversal logic.
Let's understand how to build a custom iterator in Python that generates square numbers up to a defined limit.
# Custom iterator that generates square numbers from 1 to a given limit
class SquareNumbers:
def __init__(self, limit):
self.limit = limit # Upper bound for square values
self.num = 1 # Start from number 1
def __iter__(self):
return self # The __iter__() method returns the iterator object itself
def __next__(self):
if self.num <= self.limit:
result = self.num ** 2 # Calculate square of current number
self.num += 1 # Move to the next number
return result
else:
raise StopIteration # End iteration when limit is reached
# Create an instance of the custom iterator
squares = SquareNumbers(5)
# Iterate through the custom iterator and print each square number
for square in squares:
print(square)
Output:
1
4
9
16
25
Explanation:
Also read the Strip in Python article!
You should use Python iterators when:
Understanding the distinction between an iterator and an iterable is essential when working with loops and data structures in Python. While the two are closely related, they serve different purposes.
Here is a clear comparison between the two:
Feature | Iterable | Iterator |
Definition | An object that can return an iterator | An object used to iterate through an iterable |
Built-in Method | Must implement __iter__() | Must implement both __iter__() and __next__() |
Examples | Lists, Tuples, Strings, Dictionaries, Sets | Objects returned by iter() |
Can be looped using a for loop? | Yes | Yes |
Need conversion to iterate manually? | Yes, use iter() | No, it is already ready to iterate |
Can it be reused? | Yes, can create new iterator each time | No, once exhausted, it cannot be reused |
Example: Using Iterator vs Iterable in an Attendance System
Suppose we have a list of employee names, and we want to call each one for attendance manually using an iterator. (The list itself is iterable.)
# List of employee names - this is an iterable
employee_names = ["Ravi", "Arjun", "Vikram", "Manoj"]
# Convert the iterable into an iterator
attendance_iterator = iter(employee_names)
# Calling out names one by one using the iterator
print("Marking Attendance:")
print(next(attendance_iterator)) # Output: Ravi
print(next(attendance_iterator)) # Output: Arjun
print(next(attendance_iterator)) # Output: Vikram
print(next(attendance_iterator)) # Output: Manoj
Output:
Marking Attendance:
Ravi
Arjun
Vikram
Manoj
Explanation:
Must Explore: String split() Method in Python
Understanding how an iterator in Python works is essential for writing clean and efficient loops. Iterators allow you to traverse through elements in a collection one item at a time. They work with built-in types like lists, tuples, and dictionaries, and they can also be customized using classes.
By mastering Python iterators, you gain more control over data traversal. And this is especially useful when working with large datasets or implementing custom sequences.
The StopIteration statement in Python is used to signal that an iterator has no more items to return. When the __next__() method raises this exception, it stops the loop. This exception is automatically handled when using a for loop. However, when calling next() manually, you should handle it using a try-except block to avoid unexpected crashes.
To iterate through an iterable object in Python, you can use a for loop or convert it into an iterator using the iter() function. Once it's an iterator, you can use next() to access elements one by one. This gives you more control over how and when items are accessed.
# Example of manual iteration using iter() and next()
cities = ["Mumbai", "Delhi", "Chennai"]
city_iterator = iter(cities)
print(next(city_iterator)) # Mumbai
print(next(city_iterator)) # Delhi
print(next(city_iterator)) # Chennai
To build your own iterator in Python, create a class that implements the __iter__() and __next__() methods. This lets you generate values based on your own logic, like counting backwards or generating square numbers.
# Custom iterator to generate even numbers
class EvenNumbers:
def __init__(self, limit):
self.num = 2
self.limit = limit
def __iter__(self):
return self
def __next__(self):
if self.num <= self.limit:
result = self.num
self.num += 2
return result
else:
raise StopIteration
evens = EvenNumbers(10)
for val in evens:
print(val)
An iterable is any Python object you can loop over, like lists, strings, or sets. An iterator is an object returned by calling iter() on an iterable. Only iterators implement the __next__() method to fetch elements one at a time.
Modifying an iterator while iterating over it is not safe. Doing so can lead to skipped values, infinite loops, or runtime errors. It’s better to create a copy of the data or collect changes to apply later.
The iterator protocol in Python makes it possible to write generic code that can work with any iterable. It allows delayed or lazy computation, efficient memory use, and simplifies loop syntax with for and in.
Yes, you can. In fact, Python generators are a simplified way to create iterators. A generator function uses the yield keyword and automatically implements both __iter__() and __next__() behind the scenes.
# Generator that yields square values
def square_gen(limit):
for i in range(1, limit + 1):
yield i ** 2
for val in square_gen(3):
print(val)
Once an iterator is exhausted, it cannot be reused. Calling next() after it's done will raise StopIteration. If you need to start over, you must create a new iterator from the iterable.
Yes, you can convert an iterator to a list using the list() constructor. This is helpful when you want to store or reuse the values generated by the iterator.
nums = iter([1, 2, 3])
print(list(nums)) # Output: [1, 2, 3]
Iterators in Python use lazy evaluation, meaning they calculate values only when needed. This makes them memory-efficient, especially for large datasets or infinite sequences. You only compute one item at a time, which avoids loading everything into memory.
Using iterators with custom objects gives you full control over how values are accessed and when the sequence should stop. This is useful for tasks like pagination, file processing, or simulating data streams without preloading data into memory.
Take our Free Quiz on Python
Answer quick questions and assess your Python knowledge
Author|900 articles published
Previous
Next
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.