Search⌘ K
AI Features

Iterators

Explore how Python uses the iterator protocol to manage data sequences. Understand the roles of __iter__ and __next__ methods, the StopIteration exception, and how for loops simplify iteration. This lesson helps you master implementing custom iterators and leveraging Python’s iterable classes for cleaner and more efficient code.

We'll cover the following...

In typical design pattern parlance, an iterator is an object with a next() method and a done() method; the latter returns True if there are no items left in the sequence. In a programming language without built-in support for iterators, the iterator would be used like this:

Python 3.10.4
while not iterator.done():
item = iterator.next()
# do something with the item

In Python, iteration is available across many language features, so the method gets a special name, __next__. This method can be accessed using the next(iterator) built- in. Rather than a done() method, Python’s iterator protocol raises the StopIteration exception to notify the client that the iterator has completed. Finally, we have the much more readable for item in iterator: syntax to actually access items in an iterator instead of messing around with a while statement. Let’s look at each these in more detail.

The iterator protocol

The Iterator ...