In programming, we use sequences or collections of objects a lot, particularly in algorithms and when writing programs that manipulate data. One can think of automation scripts, APIs, data-driven apps, and other domains. In this chapter, we are going to see a pattern that is really useful whenever we have to handle collections of objects: the Iterator pattern.
Note the following, according to the definition given by Wikipedia:
Iterator is a design pattern in which an iterator is used to traverse a container and access the container's elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled.
The iterator pattern is extensively used in the Python context. As we will see, this translates into iterator being a language feature. It is so useful that the language...