Applying a function to data via starmap() and map()

The built-in map() function is a higher-order function that applies a function to items from an iterable. We can think of the simple version of the map() function, as follows:

map = (lambda function, arg_iter:     (function(a) for a in arg_iter))

This works well when the arg_iter parameter is an iterable that provides individual values. The actual map() function is a bit more sophisticated than this, and will work with a number of iterables.

The starmap() function in the itertools module is simply the *a version of the map() function. We can imagine the definition as follows:

starmap = (lambda function, arg_iter:     (function(*a) for a in arg_iter))

This reflects a small shift in the semantics ...

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.