Python Functions – map, filter, and reduce

Python provides several functions which enable afunctional approachto programming. These functions are all convenience features in that they can be written in Python fairly easily.

Functional programmingis all aboutexpressions. We may say that the Functional programming is anexpression oriented programming.

Expression oriented functions of Python provides are:

map(aFunction, aSequence)

filter(aFunction, aSequence)

reduce(aFunction, aSequence)

lambda

list comprehension

map

One of the common things we do with list and other sequences is applying an operation to each item and collect the result. For example, updating all the items in a list can be done easily with aforloop:

Since this is such a common operation, actually, we have a built-in feature that does most of the work for us. Themap(aFunction, aSequence)function applies a passed-in function to each item in an iterable object and returns a list containing all the function call results.

Since it’s a built-in,mapis always available and always works the same way. It also has some performance benefit because it is usually faster than a manually codedforloop. On top of those,mapcan be used in more advance way. For example, given multiple sequence arguments, it sends items taken form sequences in parallel as distinct arguments to the function:

As in the example above, with multiple sequences,map()expects an N-argument function for N sequences. In the example,powfunction takes two arguments on each call.

Themapcall is similar to thelist comprehension expression. Butmapapplies afunction callto each item instead of anarbitrary expression. Because of this limitation, it is somewhat less general tool. In some cases, however,mapmay be faster to run than a list comprehension such as when mapping a built-in function. Andmaprequires less coding.

IffunctionisNone, theidentityfunction is assumed; if there are multiple arguments,map()returns a list consisting oftuplescontaining the corresponding items from all iterables (a kind of transpose operation). The iterable arguments may be a sequence or any iterable object; the result is always a list:

As the name suggestsfilterextracts each element in the sequence for which the function returnsTrue. Thereducefunction is a little less obvious in its intent. This function reduces a list to a single value by combining elements via a supplied function. Themapfunction is the simplest one among Python built-ins used forfunctional programming.

These tools apply functions to sequences and other iterables. Thefilterfilters out items based on a test function which is afilterand apply functions to pairs of item and running result which isreduce.

Because they return iterables,rangeandfilterboth requirelistcalls to display all their results in Python 3.0.

As an example, the followingfiltercall picks out items in a sequence that are less than zero:

At each step,reducepasses the current product or division, along with the next item from the list, to the passed-inlambdafunction. By default, the first item in the sequence initialized the starting value.

Here’s theforloop version of the first of these calls, with the multiplication hardcoded inside the loop: