Python Iterators

Python Iterators

Welcome to codeswithpankaj.com! In this tutorial, we will explore the concept of iterators in Python. We'll cover what iterators are, how to create and use them, and provide detailed examples to illustrate their application.

Table of Contents

  1. Introduction to Iterators

  2. Why Use Iterators?

  3. The Iterator Protocol

  4. Creating an Iterator

  5. Using Built-in Iterators

  6. The iter() and next() Functions

  7. Custom Iterators

  8. Infinite Iterators

  9. Practical Examples

  10. Summary

1. Introduction to Iterators

What are Iterators?

An iterator is an object that allows you to traverse through all the elements of a collection, such as a list or a tuple, one at a time. In Python, an iterator is an object that implements the iterator protocol, consisting of the methods __iter__() and __next__().

Key Points

  • Iterators provide a way to access the elements of a collection sequentially without exposing the underlying structure.

  • Iterators are used to iterate over iterable objects like lists, tuples, and dictionaries.

2. Why Use Iterators?

  • Memory Efficiency: Iterators can be used to traverse large collections of data without loading the entire collection into memory.

  • Lazy Evaluation: Iterators compute elements as they are needed, which can improve performance for large datasets.

  • Abstraction: Iterators provide a standard interface for traversing collections, making code more flexible and reusable.

3. The Iterator Protocol

An object is an iterator if it implements the __iter__() and __next__() methods.

  • __iter__(): This method returns the iterator object itself.

  • __next__(): This method returns the next value from the collection. If there are no more items, it raises the StopIteration exception.

4. Creating an Iterator

Example: Creating a Simple Iterator

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

    def __iter__(self):
        return self

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

counter = Counter(1, 5)
for num in counter:
    print(num)

5. Using Built-in Iterators

Python provides built-in iterators for several data structures, such as lists, tuples, dictionaries, and strings.

Example: Using an Iterator with a List

numbers = [1, 2, 3, 4, 5]
iterator = iter(numbers)

print(next(iterator))  # Output: 1
print(next(iterator))  # Output: 2
print(next(iterator))  # Output: 3

Example: Using an Iterator with a String

string = "Hello"
iterator = iter(string)

print(next(iterator))  # Output: H
print(next(iterator))  # Output: e
print(next(iterator))  # Output: l

6. The iter() and next() Functions

The iter() Function

The iter() function returns an iterator object.

numbers = [1, 2, 3, 4, 5]
iterator = iter(numbers)

The next() Function

The next() function returns the next item from the iterator.

print(next(iterator))  # Output: 1
print(next(iterator))  # Output: 2
print(next(iterator))  # Output: 3

7. Custom Iterators

You can create custom iterators by implementing the __iter__() and __next__() methods.

Example: Creating a Custom Iterator

class EvenNumbers:
    def __init__(self, limit):
        self.current = 0
        self.limit = limit

    def __iter__(self):
        return self

    def __next__(self):
        if self.current <= self.limit:
            value = self.current
            self.current += 2
            return value
        else:
            raise StopIteration

even_numbers = EvenNumbers(10)
for num in even_numbers:
    print(num)

8. Infinite Iterators

Infinite iterators are iterators that can produce an infinite number of values. They are useful for generating an unbounded sequence of values.

Example: Creating an Infinite Iterator

class InfiniteCounter:
    def __init__(self):
        self.current = 0

    def __iter__(self):
        return self

    def __next__(self):
        self.current += 1
        return self.current

counter = InfiniteCounter()
for num in counter:
    if num > 10:
        break
    print(num)

9. Practical Examples

Example 1: Iterating Over a Dictionary

person = {'name': 'John', 'age': 30, 'city': 'New York'}

for key in person:
    print(f"{key}: {person[key]}")

Example 2: Using Iterators with Generators

Generators are a simple way to create iterators using the yield statement.

def fibonacci(limit):
    a, b = 0, 1
    while a < limit:
        yield a
        a, b = b, a + b

for num in fibonacci(10):
    print(num)

Example 3: Creating a Range Iterator

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

    def __iter__(self):
        return self

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

my_range = MyRange(1, 5)
for num in my_range:
    print(num)

10. Summary

In this tutorial, we explored the concept of iterators in Python, their importance, and how to create and use them. We covered the iterator protocol, using built-in iterators, the iter() and next() functions, custom iterators, and infinite iterators. We also provided practical examples to illustrate the application of iterators. Iterators are a powerful feature that enhances code efficiency and flexibility.

For more tutorials and in-depth explanations, visit codeswithpankaj.com!


This tutorial provides a comprehensive overview of Python iterators, detailing each topic and subtopic with examples and explanations. For more such tutorials, keep following codeswithpankaj.com!

Last updated