If you have been doing python, you must have definitely come across the itertools module. It might not look like it, but I can tell you that it is one of the most powerful libraries on python. If you wish to write a more efficient, clean, and more ‘pythonic’ code, I believe that itertools is a must-have tool to add to your arsenal. Here in this article we will introduce some functionalities of the library and delve into some examples to help us better understand it.
However, the thing about itertools is that it is not enough to just know the definitions of the functions it contains. The real power lies in composing these functions to create fast, memory-efficient, and good-looking code. So here we will look at definitions along with some practical examples of the functions to understand their applications.
For an overview let us look at what the module’s official documentation has to say:
The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Together, they form an “iterator algebra” making it possible to construct specialized tools succinctly and efficiently in pure Python
Itertools provides us with three different types of iterators
Let’s now dive into it
Python iterators like lists, tuples, dictionaries are exhaustive. But it is not necessary for an iterator to exhaust at some point, they can go on forever. Let’s look at the three types of infinite iterators.
We can also pass negative and floating arguments to this function.
Now use the next function to print the values.
A common use of this iterator is to supply a stream of values to map() or zip().
That was all about infinite iterators. From the above examples, it is clear that there may e situations where these can come in handy. Now let’s look at the real deal of the itertools library. The terminating iterators.
These iterators are used to work on finite sequences and produce an output based on the function used.
func
argument). If func
is supplied, it should be a function of two arguments. Elements of the input iterable may be any type that can be accepted as arguments to func
. It will become more clear with some examples.
Let’s pass the optional func
argument to accumulate.
It is very similar functools.reduce()
which returns only the last accumulated value, but that is a story for another day!
chain.from_iterabe()
which works in a similar fashion, but it takes an argument as a list of lists instead of multiple lists.
predicate
in argument returns false for the first time.
False
. If predicate is None
, it returns the items that are false.
None
, key defaults to an identity function and returns the element unchanged.
dropwhile
.
fillvalue
. Iteration continues until the longest iterable is exhausted.
These were all the terminating iterators provided by the module. You can clearly see the variety of functionalities that are provided by these iterators. These can be used in your code to make it shorter, more efficient, and also more “pythonic”.
I hope that you found these explanations easy to understand and the examples gave you the hang of it all. As you will go along, you will find more beautiful applications of these functions in solving your problems.
This wraps this article and I’ll come back with the second part of Mastering itertools where we will see the combinatoric iterators along with some more complex examples of all the functions. If you have any suggestions, feedback, or simply want to connect, I’ll be delighted to reach out. You can connect with me on LinkedIn or find some of my work on Github.
Peace.