YTread Logo
YTread Logo

Python: Lambda, Map, Filter, Reduce Functions

Jun 06, 2021
I'm jojames, 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 that were added to Python 3. They were also added to Java 8, the newest version of Java in case they use Java and are actually taken from functional programming languages ​​like Lisp, so a

lambda

function is a simple one line function and does not use DEF or return keywords like traditional Python functions. These are implicit in the function, so the whole point of a lambda function is to make it very short, simple, fast and easy to implement.
python lambda map filter reduce functions
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 is our variable name for the parameter we need to have a 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 it's a very lambda function simple, just double the value you pass, let's say we want to have two variables that are passed, we want to add x and y together so a traditional function in Python looks like this: def add X comma AND return X plus and a lambda function again we take two variables X comma Y we still need these two points here and then this is our return here our traditional function gets a little longer because we have an if-else statement so we call MX death of x comma and if Python if you wanted to, but this is a standard way and then print a maximum of 8 point 5 We 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 keyword lambda and then X comma Y or the two variables that we are going to pass and what we are going to return comes after: if-else here and this is the syntax to apply an if-else statement in the returns part of the lambda function and then again we use exactly the same syntax to call 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 The box in the middle here is our map function and we two want to move to that black box above the list of elements MN and P in a function, let's say function f and then we apply the new list that we get from this map function. the function to M we apply the function to n we apply the function to P and we got a list of elements, so that's what the map function does: it applies this function to each element in our list and returns a new list, so let's say that we have a list of elements 4 3 2 1 and we want to square each of those elements, we could define a standard Python function to square each element that we pass into a list list 1, create an empty list list 2 and to each number in list 1 add the square of list number 2 as well and then we return list 2 so it returns 16 9 4 and 1 and then this print statement will print it so that is a standard way to do it in Python using the map function we could make a screen that we have defined as the list 4 3 2 1 and then we call the map function to which we pass a function that in this case we can use a lambda function and a list that we just defined here , so the lambda function will receive a variable lambda function. as a variable called which we can use this square function that we defined here, so we pass the square function that we passed in the same list that we have here and what we get is this same list 16 9 4 and 1, then we have to convert it explicitly as a list and then we can print it so that it basically does the exact same thing using this function instead of this lambda function.
python lambda map filter reduce functions

More Interesting Facts About,

python lambda map filter reduce functions...

Alternatively, if you are familiar with list comprehensions, I will make a separate video on this. I hate doing it. hijacks this video, but these understandings are really useful for simple things like this because you can do it with a minimal amount of code, you can do basically the same job, so if I wanted to print x squared for of simple list and a print statement, the

filter

function filters elements from a sequence and will return the filtered list, so let's see that we have a black box that we also want to pass some things to, this is our filter and we are going to pass a list of elements MN and P and a condition C that we are going to apply to each element and what we get is a new list of elements to actually satisfy the condition if we have a list of elements 4 3 2 1 again and we only want to print the ones above of 2, so 4 and 3 we could define a simple traditional Python function and we will say that list 2 is equal here.
python lambda map filter reduce functions
I used a list comprehension in the traditional Python function X 4x and list 1 if result, which will be 4 and 3, our filter function here we are. Let's pass a condition and a list of the condition again. We use a lambda function for the condition. The lambda function receives this variable and a list in our condition is the lambda function is underlined, the filter will apply this condition to each element of the list and return a new list and we have to explicitly convert 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 I have here in my traditional function, if we only want to filter the elements in this list that are greater than 2, we can use a simple disk comprehension to do it, so that's An alternative way of doing it, the

reduce

d function applies the same operation to the elements of the sequence, to each element of the sequence and uses the result of each operation as the first parameter of the next operation, so if you have, say, a list of elements. em in and P and you want to pass a function f where F takes two parameters, it will first take the parameters m and in from this list and apply F to it and then use the result combined with P, so What we are doing is that the function reduction will take F from M and N and then take the result of that as the first value and P is the second value and 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 of these together so a traditional Python function looks like this, def molt list 1 and then we have a product called prod which is equal to the first element in list one and then we're going to iterate through the list, we're going to multiply the product by the ice element in the list and then we'll return the final product so look back 24 using the reduced function we define list in four three two one we pass a lambda function lambda x and y that takes two variables x and y and we return the product those the second iteration, so it will multiply the product of 4 and 3 by 2 to get 24 and then to multiply the product of that by 1 to get 24 again, so it will return 24, that's how the reduce function works.
python lambda map filter reduce functions
I hope this video is useful for you. You can get all my code on my github site here if this was helpful to you. Please like and subscribe to my channel. I'm Joe James, thanks for watching.

If you have any copyright issue, please Contact