Elena' s AI Blog

Loop like a Pro with Python Iterators

18 May 2023 (updated: 24 Aug 2026) / 34 minutes to read

Elena Daehnhardt


Midjourney AI-generated art, May 2023

If you click an affiliate link and subsequently make a purchase, I will earn a small commission at no additional cost (you pay nothing extra). This is important for promoting tools I like and supporting my blogging.

I thoroughly check the affiliated products' functionality and use them myself to ensure high-quality content for my readers. Thank you very much for motivating me to write.



TL;DR:
  • Use Python iterators for memory efficiency: list comprehensions for simple cases, generators (yield) for large datasets, itertools for advanced patterns. Create custom iterators with __iter__ and __next__.

πŸ“š This post is part of the "Python Projects & Utilities" series

Series: Python Projects & Utilities (Part 1 of 8)

Previous: Part 8 β€” Brewing with Homebrew

Next: Part 2 β€” Joking Flask App

Introduction to Python Iterators

A Python iterator is an object that lets you traverse a sequence of values one at a time without tracking an index. In this post, we’ll explore iterators in Python and learn how to use them effectively. We’ll work through basic examples of iterators and show you how to create your own. Finally, we’ll cover advanced techniques for using iterators and discuss some best practices for working with them.

Python Iterators: The Iterator Protocol Explained

An iterator is an object that implements the iterator protocol, which consists of two methods: __iter__() and __next__(). The __iter__() method returns the iterator object itself, while the __next__() method returns the next value in the sequence. If there are no more values to return, the __next__() method raises a StopIteration exception.

Here’s a simple example of using an iterator in Python:

my_list = [1, 2, 3, 4, 5]
my_iterator = iter(my_list)
next(my_iterator)
1
next(my_iterator)
2
next(my_iterator)
3
next(my_iterator)
4
next(my_iterator)
5
next(my_iterator)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
StopIteration

In this example, we create a list my_list with five values. We then create an iterator object my_iterator by calling the iter() function and passing in my_list as an argument. We can then use the next() function to retrieve each value in turn. When no more values are retrieved, the StopIteration exception is raised.

Python provides several built-in objects that are iterable, including lists, tuples, strings, and dictionaries. You can also create your own iterable objects by implementing the iterator protocol.

Creating Custom Iterators in Python

Creating your own iterators in Python is relatively simple. You must define a class that implements the iterator protocol to create an iterator. Here’s an example of a simple iterator that returns the first 10 even numbers:

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

    def __iter__(self):
        return self

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

This code works for Python 3. In this example, we define a class called EvenNumbers that implements the iterator protocol. The init() method initializes the current value to 0. The iter() method returns the iterator object itself, and the next() method returns the following even number in the sequence. If the current value is greater than 20, the StopIteration exception is raised.

To use this iterator, we can create an instance of the EvenNumbers class and then iterate over it using a for loop:

even_numbers = EvenNumbers()
for number in even_numbers:
    print(number)
2
4
6
8
10
12
14
16
18
20

In this example, we create an instance of the EvenNumbers class called even_numbers. We then use a for loop to iterate over the even numbers returned by the iterator. As you can see, the output of the for loop matches the sequence of even numbers defined in the EvenNumbers class.

Advanced Iterator Techniques: itertools, yield, and with

Now that we’ve covered the basics of iterators and how to create them, let’s look at some more advanced techniques for using iterators in Python 3.

Iterator Differences Between Python 2.7 and Python 3

πŸ”’ Subscribe to keep reading.

Python Iterator Alternatives: Comprehensions, map, filter, and zip

πŸ”’ Subscribe to keep reading.

Little projects

πŸ”’ Subscribe to keep reading.

Create a list of Bird objects

πŸ”’ Subscribe to keep reading.

Iterate over the bird objects and calculate their average speed

πŸ”’ Subscribe to keep reading.

Building a Custom Wave Iterator in Python

πŸ”’ Subscribe to keep reading.

Conclusion: Mastering Python Iterators

πŸ”’ Subscribe to keep reading.

References

πŸ”’ Subscribe to keep reading.

Subscribe to unlock the full article ❀️

I keep most of the site completely open. A few unusually detailed tutorials need a free subscriber login so I can keep publishing this kind of work.

The form below signs you up for the newsletter. It does not log you into the app β€” log in afterwards (same email) to unlock this article and download your subscriber gifts. New subscribers get an inbox mail: Set a password to unlock articles.

desktop bg dark

About Elena

Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.

Citation
Elena Daehnhardt. (2023) 'Loop like a Pro with Python Iterators', daehnhardt.com, 18 May 2023. Available at: https://daehnhardt.com/blog/2023/05/18/python-iterators/
All Posts