Transcript:
I am jojame’s in this video. I’m going to cover Lambda functions, map functions, filter functions and reduce functions in Python 3 Now these are new functions are added to Python 3 They’re also added into Java 8 the newest version of Java. In case you use Java and they’re really borrowed from functional programming languages like Lisp. So a lambda function is a simple one-line function and it does not use the DEF or return key words like traditional Python functions. Do these are implicit in the function, so the whole point of a lambda function is to make it very short, simple and quick and easy to implement an example of a simple lambda function. Let’s say we want to double the value. We could write a traditional. Python function called DEF Double X, and we return X Times 2 or a Lambda function would look like this. We use the lambda keyword, and then we have our parameter in this case. There’s one parameter it’s X, So that’s our variable name for the parameter. We must have the colon. And then the return value and the return value can be a function applied to that variable. Where here we have 2 times. X as our return. So that’s a very simple Lambda function. It just doubles the value you pass in. Let’s say we want to have two variables that are passed in. We want to add X and Y together. So a traditional function in Python would look like DEF Add X comma Y return X, plus y a lambda function again. We take in two variables. X comma Y. We still need this colon here, And then this is our return X plus y so we’re returning the sum of X and Y, and this is a simple lambda function. It does this in one line. So if we want to take the max of X and y here, our traditional function gets a little bit longer because we’ve got an if-else statement, So we call death MX of X comma. Y If X is greater than Y return X else return Y. Obviously you could write this in a shorter syntax in Python. If you wanted to. But this is a standard way and then print Max of 8 Comma 5 have to call the function so our lambda function in Python this time we’re going to assign this to a variable MX where MX is actually the lambda function itself, and we have the lambda keyword and then X comma Y or the two variables were going to pass in and what we’re going to return comes after the : X If X is greater than Y else Y, so we have an if-else statement here, and this is the syntax for applying an if-else statement in the returns, part of the Lambda function, and then again, we use exactly the same syntax for calling the MX function using the map function, we can apply the same function to each element of a sequence or normally a list and then we return the modified list, So let’s say this black box in the middle here is our map function and two. We want to pass into that black box above the list of items. Mn and P in a function, let’s say function F. And then the new list that we get out of this map function is we applied the function to M. We applied the function to N. We applied the function to P and we got a list of items, so that’s what the map function does. It applies this function to each item in our list and returns a new list, So let’s say we have a list of items 4 3 2 1 and we want to square each of those items we could define a standard. Python function to square each item we pass in a list List 1 We create an empty list List 2 and for each number in List 1 we append the square of the Number 2 list, too, and then we return List 2 so that would return 16 9 4 and 1 and then this print statement would print it out, so that’s a standard way to do it in Python. Using the map function, we could do a display we’ve defined in as the list 4 3 2 1 And then we have we call the map function. We pass into that both a function, which, in this case we can use a lambda function and a list we just defined here, So the lambda function is going to receive a variable. X, and then it’s going to return X squared. So the map function is going to iterate through list in and apply this lambda function to each item in this tin, and each item is going to be passed into the Lambda function as a variable named X and then we’ll explicitly cast the return value as a list and print it out. Alternatively, we don’t necessarily have to use a lambda function for the map function so here we use a lambda function as our function, but here we decided. Hey, we can use this square function that we defined over here, so we pass in the square function. We pass in the same list in that. We have up here and what we get back. Is this same list, 16 9 4 and 1 Then we have to explicitly cast that as a list and then we can print it, so this basically does exactly the same thing using this function instead of this Lambda function. Alternatively, if you’re familiar with list comprehensions. I’ll make a separate video on this. I hate to hijack this video, but this comprehensions are really handy for simple stuff like this because you can do with a pretty minimal amount of code. You can do basically the same work, So if I wanted to print X squared for X in in, I could just do that with a simple list comprehension. And a print statement. The filter function filters items out of a sequence and it will return the filtered list, so lets. See, we have a black box that we want to pass in some stuff too. This is our filter and we’re going to pass in both a list of items. Mn and P and a condition C That we’re going to apply to each item in what we get out of. It is a new list of items to actually satisfy the condition. If we have a list of items 4 3 2 1 again and we just want to print the ones that are over 2 so 4 & 3 We could define a simple traditional. Python function and we’ll say list 2 equals here. I used a list comprehension in traditional Python function, X 4x and List 1 if X is greater than 2 so for any values in list 1 there are over 2 it will add those to list to and will return List 2 and print out the result, which will be 4 and 3 our filter function over here we’re going to pass in a condition and a list. The condition again we used a lambda function for the condition. The lambda function receives this variable. X and it will return values where X is greater than 2 and then this is our list, so we pass in a condition and a list in our condition is Lambda function is underlined the filter will apply this condition to each item in the list and will return a new list and we have to explicitly cast the return value as a list in Python, and then we can print it and again we can use a simple list comprehension. This is almost identical to this up here. I have in my traditional function. If we just want to filter the items in this list that are greater than 2 we can use a simple disk comprehension to do that. So that’s an alternative way of doing it. The reduced function applies the same operation to items of the sequence to each item in the sequence and it uses the result of each operation as the first parameter of the next operation. So if you have, let’s say a list of items, em in and P and you want to pass in a function F where F takes two parameters, it will first take parameters M and in out of this list and it will apply F to it, and then it will use the result of that combined with P. So what we’re doing is the reduce function is going to take F of M and N and then it’s going to take the result of that as the first value and P is the second value, and it’s going to apply F to that. So let’s look at an example in case that’s not clear. We have a list 4 3 2 1 We just want to multiply all these together, so a traditional. Python function would look something like this def molt list 1 and then we have a product called prod that equals the first item in list one and then we’re going to iterate through the list. We’re going to multiply prod by that the ice item in the list and then we’ll return the final product, so look at 24 back using the reduced function. We define our list in four three two one we pass in a lambda function, Lambda X and Y that takes two variables X and Y, and we return the product those X Times Y, and this is the list that we pass in. So this reduce function will apply this Lambda function to each pair of items in the list, so it starts with 4 & 3 Then it gets 12 It uses the 12 as the first item in the second iteration, so it’s going to multiply the product of 4 and 3 by 2 to get 24 then it’s going to multiply the product of that by 1 to get 24 again, so it’s going to return 24 so that’s how the reduce function works. I hope this video is helpful for you. You can get all my code on my Github site here. If this was useful for you, please click like and subscribe to my channel. I’m Joe James. Thanks for watching you.