Functional tools in Python : lambda, map, filter, reduce.

One of the powerful features of Python is the use of functional constructs, namely:

• lambda
• map
• filter
• reduce

lambda:

The general syntax of a lambda function is quite simple:
lambda argument_list: expression
The argument list consists of a comma separated list of arguments and the expression is an arithmetic expression using these arguments. You can assign the function to a variable to give it a name.
The following example of a lambda function returns the sum of its two arguments:

>> f = lambda x, y : x + y

>> f(1,1)

map:

map() is a function with two arguments:

r = map(func, seq)

The first argument func is the name of a function and the second a sequence (e.g. a list) seq. map() applies the function func to all the elements of the sequence seq. It returns a new list with the elements changed by func.

Tips:

 Using map with lambda

>> Celsius = [39.2, 36.5, 37.3, 37.8]

>> Fahrenheit = map(lambda x: (float(9)/5)*x + 32, Celsius)

>> print Fahrenheit

[102.56, 97.7, 99.14, 100.03999999999999]

  Pandas has a ‘map’ helper function which is quite interesting

>> df[‘Gender’] = df[‘Sex’].map( lambda x: x.upper() )

>> df[‘Gender’] = df[‘Sex’].map({‘female’ : 0, ‘male’ : 1}).astype(int)

filter

The function filter(function, list) offers an elegant way to filter out all the elements of a list, for which the function function returns True.
The function filter(f,l) needs a function f as its first argument. f returns a Boolean value, i.e. either True or False. This function will be applied to every element of the list l. Only if f returns True will the element of the list be included in the result list.

>> fib = [0,1,1,2,3,5,8,13,21,34,55]

>> result = filter(lambda x: x % 2, fib)

>> print result [1, 1, 3, 5, 13, 21, 55]

>> result = filter(lambda x: x % 2 == 0, fib)

>> print result [0, 2, 8, 34]

reduce

>> reduce(lambda x,y: x+y, [47,11,42,13])

113

>> f = lambda a,b: a if (a > b) else b >>> reduce(f, [47,11,42,102,13])

102