Simplifying Code through Iterators

Learn how to improve code through repeated iterations and nested loops.

We'll cover the following

Now, let's briefly discuss some situations that can be improved with the help of iterators and occasionally the itertools module. After discussing each case, and its proposed optimization, we'll close each point with a corollary.

Repeated iterations

Now that we have seen more about iterators and introduced the itertools module, we can show you how one of the first examples of this chapter (the one for computing statistics about some purchases) can be dramatically simplified:

Get hands-on with 1200+ tech skills courses.