What Are Python Iterators? See Examples in detail

In Python, iterators are objects that allow you to traverse through elements of a collection one at a time. They are used with loops or functions like next() to access items lazily, which is memory-efficient for large datasets.

An iterator is created from an iterable (like a list, tuple, or set) using the iter() function.


Why Iterators Are Important

  • Enable lazy evaluation for memory efficiency
  • Allow traversal of large datasets without loading everything at once
  • Used extensively in Python’s for loops and generator functions
  • Useful in real-world applications like streaming data

Example:

nums = [1, 2, 3]
it = iter(nums)
print(next(it))  # Output: 1
print(next(it))  # Output: 2

Creating an Iterator

my_list = [10, 20, 30]
my_iter = iter(my_list)

print(next(my_iter))  # 10
print(next(my_iter))  # 20
print(next(my_iter))  # 30

Attempting next() again will raise StopIteration.


Example 1: Using Iterators in Loops

fruits = ["apple", "banana", "cherry"]
for fruit in iter(fruits):
    print(fruit)
  • The for loop automatically uses an iterator under the hood.

Example 2: Creating a Custom Iterator

class Count:
    def __init__(self, low, high):
        self.current = low
        self.high = high

    def __iter__(self):
        return self

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

counter = Count(1, 5)
for number in counter:
    print(number)

Output:

1
2
3
4
5

Example 3: Real-World Scenario – Streaming Data

Suppose you want to process lines from a large log file without loading it all in memory:

with open("large_log.txt") as file:
    lines = iter(file)
    for _ in range(5):  # process first 5 lines
        print(next(lines).strip())

Advantages of Iterators

  • Memory-efficient for large datasets
  • Can represent infinite sequences
  • Simplifies lazy computation pipelines

Best Practices

✔ Always handle StopIteration when using next() manually
✔ Use iterators with large datasets or streams
✔ Combine with generator functions for better memory efficiency


Conclusion

Iterators in Python provide a powerful, memory-efficient way to traverse elements of a collection. Mastering iterators is essential for working with large datasets, streaming data, and building scalable Python applications.


References

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *