The itertools module is one of the cornerstones of advanced programming in Python. Part of the standard library, it provides an iterator algebra that allows us to elegantly chain abstractions, enabling solutions that are both simpler and more memory efficient.
The goal of this talk is to offer practical advice and clear lessons that can be immediately put into practice. Illustrating it with numerous examples, attendees will leave having assimilated at least several concepts that will improve their code undeniably and irremediably. Emphasis will be on showing specific cases where a traditional solution can be overhauled over and over with functions from the itertools module.
Let’s say, for example, that we want to alternate indefinitely between two values: -1 and 1. The novice would use a variable, updating its value at each step, and the average user would maybe opt for an endless generator. Both are worthy and honorable solutions, but they pale before the mastery of the martial artist who only needs itertools.cycle(). Because that is the nature of the itertools module: once the veil falls from our eyes and we come across functions such as repeat(), takewhile(), dropwhile() or product(), there is no return. In this talk we will learn to identify when they can be used, accomplishing with a single line of code what for the mere mortals takes much more effort.