## Python Tutorial for Beginners 43 – Lambda, filter, reduce and map

hey guys welcome to the next blog on python tutorial for beginners in this blog I’m going to show you how to use lambda functions in Python in addition I’m going to show you how to use lambda functions with functions like Map Reduce and filter so let’s get started so as we all know that Python is a multi paradigm language and it also supports functional programming and the lambda functions falls into functional programming paradigm so what are lambda functions and how to use lambda functions so lambda functions in Python are also called anonymous functions because they don’t have any name sometimes they are also called one-line functions because they can be written in a single line of code so let’s see how we can define a lambda function so as you can see here I have three normal function defined here one is the double functions which doubles whatever arguments you provide here second is a simple addition function and third function is the function which takes three arguments and provide the product of the three argument which are provided so let’s see how we can convert these three functions into the lambda functions so the first function which we have is that double function so I’m going to once again write double as a variable and then I can use a special keyword called lambda to create a lambda function so as you can see here this double takes one argument which is X so to provide an argument in the lambda function you just write the name of the argument and then after the colon you write the body of the function so in our case this double function have this body which is return x x two so in lambda function if you want to return some value you don’t need to use this return keyword you can directly write x x 2 here so this lambda function is equivalent to this double function which is a normal function so this w function takes one argument which is X which we are providing here and it returns the multiplication of X by two which we are writing after the Kulin now let’s write the add function using lambda function so I’m going to once again declare a variable called add and once again used lambda and this time this add takes two arguments so I can provide multiple arguments in lambda functions by comma as you do with the normal function so I will provide two arguments and then after the colon you just write whatever you want to return so we want to return X plus y so we can simply write x plus y here and that’s it so in a single line we can define the lambda function now the third function we want to provide here is the product function so I will declare a variable product and this time I just need to provide three arguments using the comma separator and after the colon I just provide the multiplication of these three values which is X x y x said so all these three normal functions is equivalent to these lambda functions so I’m going to just comment these three functions out by pressing ctrl + forward slash and now let’s see how to use these lambda functions so you create a lambda function and then assign the result into some variable and then you use these variable as you use the normal functions so I’m going to just use print to call the double variable and then using the parentheses I just provide the argument which this lambda function takes so lambda function takes one argument which is X so I can provide the value of x here so let’s say I want to provide 10 as the value of X same I will do with the add a function and the product function so add a function takes two argument and the product function takes three argument so I can simply write these functions like this like a normal function and then I can just run my script and you will see it’s going to give us the same result as the normal function gives us so the first is the double of ten second is the addition of these two numbers and third is the product of these three numbers now you might ask a question that these functions which we have defined normally we can also define these function in a single line because there is not much logic inside these functions so we can declare these functions like this and they are also the single line functions so why do we use lambda functions so the lambda functions are generally used with the functions which takes function as an argument or returns function as the result so in functional programming functions are the first-class citizens that means we can pass the functions as the normal argument so as you can see here we have passed some static or constant value as an argument here but we can also pass the functions as an arguments and in addition you can also return a function from a function so instead of this multiplication you can create a function and return from a function and that’s where these lambda functions are useful so let me give you some examples of where you can use lambda functions so as I said you can use lambda functions with the functions like filter reduce and map so let me illustrate how these functions work so I’m going to declare a list of some numbers so now I have created a list which have these six elements now let’s see how to use a map function with a lambda function so there is an inbuilt function called map in Python which takes two argument one is a function and second is an iterate able the variable and we all know that collections like lists or tuples or the dictionaries are all iterate able collections so we can use map keyword and then here we can define a lambda function so let’s say I want to double each and every element of this list so I can define a function which takes one argument X and then we are going to return that double of this value so this is the function which we have passed to this map function because the first argument which map expects is the function the second is the iterate able collection so we are going to pass this list as the second value let me define let’s say my list here so it will be more clearer so I’m going to just pass this my list as the second variable so what this map function is going to do is it’s going to apply this lambda function to each and every element of this list which is basically going to double the value of each and every element of the list now let’s assign the result of this map function into our variable so I’m going to assign the result into some variable and then I’m going to print the result first of all so this variable contains the map function so let’s see first of all what is the result and then I’m going to show you how to get the result which we are expecting so you can see here whenever you want to print the value of map it’s going to just give you this kind of result so how we can get the list out of this map value in order to get the list out of this map value you need to cast this value into a list and how to convert this value into a list you use a list function and then pass the result of the map function as an argument of this list function let’s run the program once again and you will see now that the result is a list and in the result every element of the list is doubled so basically what this map function has done is it has applied this function to each and every value of the list and this function just doubles each and every value of the list now you might say that this is a very simple function which we are applying to the list so let’s make things a little bit complex and let’s say now I have two lists one is my list 1 and other is my list too and this other list contains some other set of elements so let me just change the values which this second list contains and let’s say using this map function I want to add each and every element of these two lists and create a third list with the addition of let’s say 2 + 1 + 5 + 4 8 + 7 10 and 8 like this so how can I create a map function to achieve this so let’s say this is a variable B and here inside the lambda function we gave the second argument which is y so it takes two argument and then it adds the value of x + y so we can do like this and then as the second argument we are passing the first list and also as the third argument you can pass the second list here so now what this map function is going to do is it’s going to take the first element of the first list and the first element of the second list and then it’s going to apply this function to the first elements of both these lists which is to add these two numbers so let’s see what is the result which we get using this map function so I’m going to just convert the answer or the result into a list and then I’m going to just pass this B variable as an argument of this list and then we are just printing the results so let’s run the code and let’s see what happens so you can see we get the list which adds the value of these two lists and creates a new list so you can see 2 plus 1 is 3 here 5 plus 4 is 9 then 8 plus 7 is 15 and so on so this is how you can use lambda functions with math function now let’s see how to use a filter function so I’m going to use this special function which is called filter and this filter functions takes two argument but what is special about this filter function is it takes a function as the first argument which gives us a boolean result so instead of adding two numbers and returning this filter function is expecting a function which gives us the boolean result so let me minimize this so let’s say we want to filter the even values from this list so how we can achieve this using the filter function so as I said this takes a function as the first argument so you can use lambda and how to find out the even values you pass an argument and then you return X modulo 2 is equal to 0 so this X modulo 2 is going to give us 0 whenever the number is even otherwise is going to give us 1 so this is the first argument and the second argument we can pass here is for example the first list here and now I’m going to assign the result into a variable and let’s print the value of C now using the list function so list and as an argument I’m going to pass C here let’s run the code and let’s see what’s the result and you can see you just get the even values filtered out from this first list which is 2 8 and 10 now let me give you one more example of filter so let’s say you want to filter out from the second list which values are greater than 5 so all the values which are greater than 5 we want to filter out so we will pass X as an argument and let’s say this is the D variable so this lambda function takes X as an argument it’s going to return true so let me just write the syntax here so it’s going to return true if the value of x is greater than 5 else it’s going to return the false so else we can just write false here so if you want to write if an else condition inside a lambda function you write something like this so this is the syntax of if and else so if the value of X is greater than 5 it’s going to return true otherwise it’s going to return false so let’s run this code and let’s see what happens let me just replace the C by D and I’m going to run this file and you will see the result here so in this list you will see 8 10 and 9 are greater than 5 that’s why we got this result if we write here greater than and equal to and once again run the code it’s going to give us 4 values including 5 here so this is how you can use filter function with the lambda functions now let’s see how we can use one more function which is called the reduced function so in order to use the reduce function we need to import a special module which is Fung tool so let’s import this module and this module is called func tools so we are going to write from Fung tool import reduce so what we are basically saying is we need a reduce function from this Fung tool module and now you can use a reduce function so I’m going to declare a variable E and then I can use this reduce function which also takes two argument one is the function and other is the iterative collection so let’s define a function first of all which is the lambda function and this lambda function is going to take let’s say two values x and y and what it’s going to return is the sum of X plus y here now as the second argument I’m going to pass my list variable here so what a reduce function does is it takes first two element of the list and then apply the function which you have written here which is going to add first two element and then it’s going to store the result in X variable and take next element into the Y variable so seven plus y is going to give us whatever result and once again it’s going to apply the result and then going to add the next number to the result so let’s see what result we get using this reduce function so I’m going to once again use print and then we can directly print the value of the reduced function which is e so let’s run this code once again and you will see this result which is 37 now 37 is the sum of all these elements inside this my list so let me explain you once again what this reduced function is doing so first of all because we are passing two arguments here in the lambda function it’s going to take first two element of your collection which is two and five and then apply this code or statement to these two elements which is two plus five we will get seven as the result now once again it’s going to take seven as the first argument which is the sum of these two numbers and eight as the second argument which is why here and then apply this logic once again to seven and eight which gives us fifteen and once again this fifteen will be passed as X and 10 will be passed as Y and once again the addition of 15 and 10 will happen which will make it 25 and so on so this is how you can use lambda functions in Python and this is how you can use lambda functions with map filter and reduce functions so I hope you have enjoyed this blog and I will see you in the next blog