python

map, filter and reduce with lambda in Python

Python has three really useful functions to use with lambda called map, reduce and filter.

I would like to highlight, some basic use of these functions in python:

lambda:

lambda is utility to quickly create the function in one line. You can consider it as one line function as well. Let’e take example:

Let’s say if you want to generate numbers from 0 to 4 using for loop, you would do something like this:

for item in range(5): print item  //0, 1, 2, 3, 4
              OR
[item for item in range(5)]     // [0, 1, 2, 3, 4]

if I want to do same using lambda, I would write this way:

numbers  = lambda x: x
print numbers(range(5))            // [0, 1, 2, 3, 4]

Writing above is same in function as this:

def numbers(x):  return x
    print numbers(range(5))

In above statement, numbers is kind of function which return us the values which been calculated by lambda.

 Beside for loop, you can also do multiple things with lambda,some of 
 example could be as:
  •  sum = lambda x, y: x+y    // lambda function will sum any two numbers
  •    print sum(4, 5)         // 9 is sum for the passed values
  • Use of 'if and else' in lambda:
     even = lambda x: if x%2 == 0 else None   
     //Return number if even else None
     print even(10)   //10

Let’s look into the function which could really be handy if you want to use lambda:

map():

This is one of most used function for me with lambda. map is function which iterate over each value in list/string etc… so as example, let say you want to multiply each value of array with 2, you would the write code using map and lambda:

print map(lambda x: x*2, range(5))          //[0, 2, 4, 6, 8]
Explanation:
range basically generate a list from 0 to 4 and map function pass 
each value from 0 to 4 to lambda and multiply by 2 which build a 
list of multiplication of values by 2

map works same as it sound like. map function basically map each value from iterator/list and send to the function. you can also write above code this way:

def multiply(num):
    return num * 2

// it's fetching each value from list and passing as parameter to multiply.
print map(multiply, range(5))  

filter():  

filter is basically performing if and else condition for you in the lambda. You can write your code without using filter. It basically to fetch the values with specific condition. So, if you take the same example as above for finding even and odd number, we can write using filter this way:

print filter(lambda x: x%2, range(5))  //1, 3
Explanation:
range create a list from 0 to 4 and each value from list pass to 
lambda function which check if value divide by 2 if not, it store
that value in list.
//If you don't want to write if and else, you can directly use filter 
 to do the same

reduce():

Main functionality of reduce is to reduce the function of one value. So most use of reduce when you want to sum all the value of list/iterator or you want to perform the calculation with all the values of list/iterator to find one value:

    print reduce(lambda x,y: x+y, range(5))
    
Explanation:
range create a list from 0 to 4 and each value from list pass to 
lambda function which add each value to another value in the list
and keep adding until all the values has been added. This is the way
it's add the values in list:
0 (x) + 1(y) = 1(x) + 2(y) = 3(x) + 3(y) = 7(x) + 4(y) = 11
      

Hope, this tutorial help to understand the lambda concept and functions. Please provide your feedback and feel free to email me for any questions.

 

 

Advertisements

2 thoughts on “map, filter and reduce with lambda in Python

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s