Lambda, Map, Filter and Reduce built in functions

--> --> Lambda, Map, Filter and Reduce

Lambda means anonymous function in python..
That’s fine, why we need a function without a name? Then how to use/call it?
Answer you will get it soon, before that we will learn features and syntax of lambda functions.

I believe examples depicts a lot J

Eg 1: lamba function with one argument x and one return value
>>> lambda x:x*x
<function <lambda> at 0xb76a36bc>
>>> fun = lambda x:x*x
>>> fun(12)
144

Syntax : lambda arg1, arg2, ...argN : expression using arguments

Eg 2: lambda function with one argument “x” and three return value
>>> fun = lambda x:(x*x,  x**x,  x+x)
>>> fun(12)
(144,  8916100448256L,  24)

Eg 3: lambda function with Two argument “x and y” and three return value
>>> fun = lambda x, y:(x*y,  x**y, x+y)
>>> fun(12, 10)
(120,  61917364224L,  22)

Eg 4: lambda function with default arguments
>>> fun = (lambda x=10, y=22:(x*y, x**y, x+y))
>>> fun(12, 10)
(120, 61917364224L, 22)
>>> fun()
(220, 10000000000000000000000L, 32)

Eg 5: lambda function with conditional arguments
>>> min_lambda = (lambda x, y: x if x < y else y)
>>> min_lambda(2,3)
2

Eg 6: lambda function with in lamba function
>>> rec_lambda = (lambda x: (lambda newx: x + newx))
>>> fun = rec_lambda(1)
>>> fun(3)

That’s more than enough about lambda, now will see how to use lambda functions so efficiently.
Well it’s time to explore Map, filter and reduce

These are functions provided by Python, we can call them built-in functions.
It is recommended to use these function in most of the cases to achieve optimizations

Map: when you need to apply an operation on each item of the list and generate an list you need map.

For example consider you have a list [1,2,3] you need to square them [1,4,9] what will you do ?
Iterate over each element using for loop and do square each element and create new list?
Never do that.. jus use map()

Syntax:  map(aFunction, aSequence)

Eg:
>>> def sq(x):
...         return x**2
...
>>> map(sq,[1,2,3])
[1, 4,9]

Can we use lambda functions here? Just think  J
Well it’s simple.

map(lambda x:x**2,[1,2,3])   # super cool isn’t it ?

Tats fine, what if I had multiple lists to be mapped to one list?
Never worry

>>> li_x = [1, 2, 3, 4, 5]
>>> li_y = [9, 8, 7, 6, 5]
>>> map(lambda x, y : x*y, li_x, li_y)
[9, 16, 21, 24, 25]

Note: If lambda function in map takes “n” arg then you need to have “n” input sequences to be feed to map() function.

Filter:
Filter just extracts each element in the sequence for which your function returns True.

Syntax: filter(aFunction, aSequence)

Say for example you need to filter out even numbers from given sequence.

X = [1, 2, 3, 4, 5, 6, 7, 8, 9]
filter(lambda y: True if y % 2 == 0 else False, x)    #or
filter(lambda y: y % 2 == 0, x)

will return you the list with only even numbers…

Reduce:
Similarly reduce is used to reduce the given sequence to a single result.
Example : to compute sum of all the element in sequence

reduce( (lambda x, y: x + y), [1, 2, 3, 4,5] )

reduce passes the current sum in the example to next item of the sequence, to the passed-in function.