Starmapping with operators

The itertools.starmap() function is a variation on the map() higher-order function. The map() function applies a function against each item from a sequence. The starmap(f, S) function presumes each item, i, from the sequence, S, is a tuple, and uses f(*i). The number of items in each tuple must match the number of parameters in the given function.

Here's an example:

>>> d = starmap(pow, zip_longest([], range(4), fillvalue=60))  

The itertools.zip_longest() function will create a sequence of pairs, such as the following:

[(60, 0), (60, 1), (60, 2), (60, 3)]

It does this because we provided two sequences: the [] brackets and the range(4) parameter. The fillvalue parameter will be used when the shorter sequence runs ...

Get Functional Python Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.