Itertools

Learn how to utilize the itertools module in Python.

Working with iterable objects has the advantage that the code blends better with Python itself because iteration is a key component of the language. Besides that, we can take full advantage of the itertools module. Actually, the sequence() generator we just created is fairly similar to itertools.count(). However, there is more we can do.

Use of itertools

One of the nicest things about iterators, generators, and itertools is that they are composable objects that can be chained together.

Naïve implementation

For example, going back to our first example that processed purchases in order to get some metrics, what if we want to do the same, but only for those values over a certain threshold? The naïve approach to solving this problem would be to place the condition while iterating:

Get hands-on with 1200+ tech skills courses.