Introduction
Python provides powerful tools for working with collections of data. Whether you’re looping through a list, tuple, string, dictionary, or file, Python uses a concept called iteration behind the scenes. The core component that makes iteration possible is the iterator.
An iterator is an object that allows you to traverse through elements one at a time without needing to know how the data is stored internally. Iterators are memory-efficient because they generate values on demand rather than loading everything into memory at once.
For example, when you use a for loop:
numbers = [10, 20, 30]
for num in numbers:
print(num)
Note: Python automatically converts the list into an iterator and retrieves each element one by one.
What are Python Iterators?
An iterator is an object that allows sequential access to elements in a collection.
It retrieves one item at a time and remembers its current position.
Example:
numbers = [1, 2, 3, 4]
iterator = iter(numbers)
print(next(iterator))
Output:
Explanation:
- iter() creates an iterator object.
- next() retrieves the next element.
Iterable vs Iterator
Many beginners confuse iterables and iterators.
| Iterable | Iterator |
|---|---|
| Collection of data | Object that traverses data |
| Can create iterator | Retrieves values one by one |
| Examples: List, Tuple, String | Created using iter() |
| Uses iter() | Uses next() |
Iterable Example:
numbers = [1, 2, 3]
A list is an iterable.
Iterator Example:
iterator = iter(numbers)
Now it becomes an iterator.
How Iterators Work
Python iterators use two special methods:
iter()
Returns the iterator object.
next()
Returns the next value.
When no values remain, Python raises StopIteration.
Creating an Iterator
Example:
fruits = ["Apple", "Banana", "Mango"]
iterator = iter(fruits)
print(next(iterator))
print(next(iterator))
print(next(iterator))
Output:
Banana
Mango
Each call to next() returns the next item.
What Happens When Items End?
Example:
numbers = [1, 2]
iterator = iter(numbers)
print(next(iterator))
print(next(iterator))
print(next(iterator))
Output:
2
StopIteration
The iterator raises a StopIteration exception when all elements are exhausted.
Using Iterators with for Loops
Most of the time, iterators are used automatically.
Example:
numbers = [10, 20, 30]
for num in numbers:
print(num)
Output:
20
30
Internally Python performs:
iterator = iter(numbers)
while True:
try:
print(next(iterator))
except StopIteration:
break
Iterating Through Strings
Strings are iterable objects.
Example:
text = "Python"
iterator = iter(text)
print(next(iterator))
print(next(iterator))
Output:
y
Iterating Through Tuples
Example:
colors = ("Red", "Green", "Blue")
iterator = iter(colors)
print(next(iterator))
Output:
Iterating Through Dictionaries
Example:
student = {
"name": "John",
"age": 20
}
iterator = iter(student)
print(next(iterator))
Output:
By default, dictionary iteration returns keys.
Creating a Custom Iterator
Python allows you to create your own iterator classes.
Example:
class Counter:
def __init__(self):
self.num = 1
def __iter__(self):
return self
def __next__(self):
if self.num <= 5:
value = self.num
self.num += 1
return value
raise StopIteration
Usage:
counter = Counter()
for value in counter:
print(value)
Output:
2
3
4
5
Understanding iter()
The __iter__() method returns the iterator object.
Example:
def __iter__(self):
return self
This tells Python that the object itself acts as an iterator.
Understanding next()
The __next__() method returns the next item.
Example:
def __next__(self):
return value
If no more items exist, raise StopIteration must be used.
Infinite Iterators
Iterators can generate endless values.
Example:
class InfiniteNumbers:
def __init__(self):
self.num = 1
def __iter__(self):
return self
def __next__(self):
value = self.num
self.num += 1
return value
Usage:
numbers = InfiniteNumbers()
print(next(numbers))
print(next(numbers))
print(next(numbers))
Output:
2
3
Be careful because such iterators never stop automatically.
Real-Life Examples:
1. File Reading
Files are iterators in Python.
Example:
file = open("data.txt")
for line in file:
print(line)
Benefits:
- Reads one line at a time
- Saves memory
- Efficient for large files
2. Employee ID Generator
class EmployeeID:
def __init__(self):
self.id = 1000
def __iter__(self):
return self
def __next__(self):
self.id += 1
return self.id
Usage:
employee = EmployeeID()
print(next(employee))
print(next(employee))
Output:
1002
Useful for generating unique IDs.
3. Countdown System
class Countdown:
def __init__(self, start):
self.start = start
def __iter__(self):
return self
def __next__(self):
if self.start <= 0:
raise StopIteration
current = self.start
self.start -= 1
return current
Usage:
count = Countdown(5)
for num in count:
print(num)
Output:
4
3
2
1
Built-in Functions Related to Iterators
iter()
Creates an iterator.
iterator = iter([1, 2, 3])
next()
Returns the next item.
next(iterator)
enumerate()
Creates an iterator with index values.
names = ["A", "B"]
for index, name in enumerate(names):
print(index, name)
Output:
1 B
Iterator Protocol
To create a valid iterator, a class must implement:
| Method | Purpose |
|---|---|
| iter() | Return iterator object |
| next() | Return next value |
This is known as the Iterator Protocol.
Advantages of Iterators
| Advantage | Description |
|---|---|
| Memory Efficient | Processes one item at a time |
| Faster for Large Data | Avoids loading all data |
| Lazy Evaluation | Generates values when needed |
| Easy Traversal | Works with loops |
| Reusability | Useful in custom classes |
Disadvantages of Iterators
| Disadvantage | Description |
|---|---|
| Single Pass | Cannot restart automatically |
| More Complex | Custom iterators require extra code |
| Exhaustion | Cannot reuse once completed |
Common Mistakes
1. Forgetting StopIteration
Incorrect:
def __next__(self):
return self.num
Without raise StopIteration the loop may never end.
2. Calling next() Too Many Times
Incorrect:
iterator = iter([1])
next(iterator)
next(iterator)
Output:
Always handle exhaustion properly.
3. Confusing Iterable and Iterator
Incorrect:
numbers = [1, 2, 3]
next(numbers)
Output:
Correct:
iterator = iter(numbers)
next(iterator)
4. Missing iter()
A custom iterator must implement:
__iter__()
Otherwise, it won’t work correctly with loops.
5. Returning Wrong Values
Always return the next item from __next__().
Conclusion
Python Iterators are a fundamental concept that enables efficient data traversal and lazy evaluation. They allow elements to be processed one at a time, making them ideal for large datasets, file handling, data streams, and custom data structures.
Python’s iterator protocol, built around the __iter__() and __next__() methods, provides a flexible way to create custom iteration behavior.
Python Iterators – Interview Questions
Q 1: What is an iterator in Python?
Q 2: How do you get an iterator from a collection?
Q 3: How do you get the next value from an iterator?
Q 4: What happens when an iterator is exhausted?
Q 5: Can all iterables be used as iterators?
Python Iterator – Objective Questions (MCQs)
Q1. What is an iterator in Python?
Q2. Which method is used to get the next item from an iterator?
Q3. What is the purpose of the iter() function in Python?
Q4. What will happen if you call next() on an iterator that has no more elements?
Q5. Which of the following is true about Python iterators?