How to Build a basic Iterator in Python?
Last Updated :
03 Jun, 2024
Iterators are a fundamental concept in Python, allowing you to traverse through all the elements in a collection, such as lists or tuples. While Python provides built-in iterators, there are times when you might need to create your own custom iterator to handle more complex data structures or operations. In this article, we will explore how to build a basic iterator in Python from scratch.
What is an Iterator?
An iterator in Python is an object that implements two methods: __iter__() and __next__(). The __iter__() method returns the iterator object itself and is called once at the beginning of an iteration. The __next__() method returns the next value from the sequence and raises a StopIteration exception when there are no more items to return.
Define the Iterator Class:
- __init__: The constructor initializes the maximum value (max_n) and the current value (current), which starts at 0.
- __iter__: This method returns the iterator object itself. It is called once at the beginning of the iteration.
- __next__: This method calculates the square of the current value, increments the current value, and returns the result. If the current value exceeds max_n, it raises a StopIteration exception to signal the end of the iteration.
Why Use Custom Iterators?
Custom iterators allow you to:
- Encapsulate complex iteration logic.
- Maintain state between iterations.
- Generate values on the fly (lazy evaluation), which can be more memory-efficient for large datasets.
Example 1: Building a Basic Custom Iterator
Let's build a basic iterator that generates a sequence of square numbers. We'll start by creating a class that implements the iterator protocol.
Initialization: The SquareIterator class is initialized with a maximum value max_n, which determines how many squares will be generated. The current attribute keeps track of the current number being squared.
Iteration: The __iter__ method is straightforward; it returns the iterator object itself (self). This method is necessary to make our object an iterator.
Generating Values: The __next__ method is where the core logic resides. It checks if the current value has exceeded the maximum (max_n). If it has, it raises a StopIteration exception, which stops the iteration. Otherwise, it calculates the square of the current value, increments current, and returns the squared value.
Python
class SquareIterator:
def __init__(self, max_n):
self.max_n = max_n
self.current = 0
def __iter__(self):
return self
def __next__(self):
if self.current > self.max_n:
raise StopIteration
result = self.current ** 2
self.current += 1
return result
# Using the custom iterator
square_iter = SquareIterator(5)
for num in square_iter:
print(num)
Output:
0
1
4
9
16
25
Example 2: Building a Fibonacci Iterator
We'll create an iterator that generates Fibonacci numbers up to a specified limit.
- Class Definition:
FibonacciIterator
is defined with an __init__
method that initializes the starting values of the Fibonacci sequence (self.a
and self.b
) and the limit up to which the numbers should be generated. - Iterator Method: The
__iter__
method returns the iterator object itself. - Next Method: The
__next__
method generates the next number in the Fibonacci sequence. If the current number exceeds the specified limit, it raises a StopIteration
exception. - Using the Iterator: The iterator can be used with a
for
loop or the next()
function to generate and print Fibonacci numbers up to the specified limit.
Python
class FibonacciIterator:
def __init__(self, limit):
self.limit = limit
self.a = 0
self.b = 1
def __iter__(self):
return self
def __next__(self):
if self.a > self.limit:
raise StopIteration
current = self.a
self.a, self.b = self.b, self.a + self.b
return current
# Create an instance of FibonacciIterator
fib_iterator = FibonacciIterator(100)
# Using a for loop to iterate
print("Using for loop:")
for num in fib_iterator:
print(num)
# Using next() to iterate
print("\nUsing next() function:")
fib_iterator = FibonacciIterator(100)
print(next(fib_iterator))
print(next(fib_iterator))
print(next(fib_iterator))
print(next(fib_iterator))
print(next(fib_iterator))
print(next(fib_iterator))
print(next(fib_iterator))
print(next(fib_iterator))
print(next(fib_iterator))
print(next(fib_iterator))
print(next(fib_iterator))
print(next(fib_iterator))
Output:
Using for loop:
0
1
1
2
3
5
8
13
21
34
55
89
Using next() function:
0
1
1
2
3
5
8
13
21
34
55
89
Conclusion
Building a custom iterator in Python is a powerful way to create more complex and memory-efficient data traversal mechanisms. By implementing the __iter__ and __next__ methods, you can control the iteration logic and manage state between iterations. The example provided here demonstrates how to create a simple iterator that generates square numbers, but the principles can be applied to more complex scenarios as well.
Similar Reads
How to iterate over files in directory using Python? Iterating over files in a directory using Python involves accessing and processing files within a specified folder. Python provides multiple methods to achieve this, depending on efficiency and ease of use. These methods allow listing files, filtering specific types and handling subdirectories.Using
3 min read
Iterate over a set in Python The goal is to iterate over a set in Python. Since sets are unordered, the order of elements may vary each time you iterate. You can use a for loop to access and process each element, but the sequence may change with each execution. Let's explore different ways to iterate over a set.Using for loopWh
2 min read
Combinatoric Iterators in Python An iterator is an object that can be traversed through all its values. Simply put, iterators are data type that can be looped upon. Generators are iterators but as they cannot return values instead they yield results when they are executed, using the 'yield' function. Generators can be recursive jus
4 min read
Iterate over a tuple in Python Python provides several ways to iterate over tuples. The simplest and the most common way to iterate over a tuple is to use a for loop. Below is an example on how to iterate over a tuple using a for loop.Pythont = ('red', 'green', 'blue', 'yellow') # iterates over each element of the tuple 't' # and
2 min read
Infinite Iterators in Python Iterator in Python is any python type that can be used with a âfor in loopâ. Python lists, tuples, dictionaries, and sets are all examples of inbuilt iterators. But it is not necessary that an iterator object has to exhaust, sometimes it can be infinite. Such type of iterators are known as Infinite
2 min read
How to check if an object is iterable in Python? In simple words, any object that could be looped over is iterable. For instance, a list object is iterable and so is an str object. The list numbers and string names are iterables because we are able to loop over them (using a for-loop in this case). In this article, we are going to see how to check
3 min read
How to Initialize a List in Python Python List is an ordered collections on items, where we can insert, modify and delete the values. Let's first see how to initialize the list in Python with help of different examples. Initialize list using square brackets []Using [] we can initialize an empty list or list with some items. Python# I
2 min read
Iterators in Python An iterator in Python is an object that holds a sequence of values and provide sequential traversal through a collection of items such as lists, tuples and dictionaries. . The Python iterators object is initialized using the iter() method. It uses the next() method for iteration.__iter__(): __iter__
3 min read
Iterate over a list in Python Python provides several ways to iterate over list. The simplest and the most common way to iterate over a list is to use a for loop. This method allows us to access each element in the list directly.Example: Print all elements in the list one by one using for loop.Pythona = [1, 3, 5, 7, 9] # On each
3 min read
Python - Itertools.chain() The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easily. One such itertools function is chain().Note: For more information, refer to Python Itertools chain() function It is
4 min read