Lambda functions by dint allows us to create anonymous functions meaning functions having no names.
Using the facility known as lambda function and lambda functions small functions usually not more than
in line.
And this is the syntax for a lambda function.
Here to have define a normal function and a normal function is defined using the keyword death.
And then we have the name of the function followed by the parameters parsed in parentheses and function
header and the body are separated by a colon and then this particular function.
We are returning the sum of these parameters are the arguments pass to this function.
Now if we want to return the same result using a lambda function here we have the lambda function when
you want to use a lambda function you start with D key word lambda and we know lambda functions do not
have a name.
So next we pass in the argument by separating them with commas and the argument and the function body
are separated by a colon and thus the function body is also known as an expression and this expression
is evaluated by Python and the result put it on.
And when Python executes a lambda function it evaluates this expression and automatically it dawned
it results lambda.
Definition does not include that or don't statement it always contains an expression which is done.
So there's always an implicit return statement.
A lambda function can have any number of arguments unlimited number of arguments like V bars to a normal
function but it should contain only one expression which is evaluated.
So here we have a lambda expression which evaluates the expression explicit why and how do you parse
arguments to a lambda function.
You can parse arguments to a lambda function see if you want to parse the arguments 2 and 3 you can
parse like this and this expression has Red Dawn the result 5 or you can also assign the lambda function
to a variable R and identify you like this.
And if you want to parse arguments to this lambda function you can do this like we do for the normal
function say the argument at 2 3 and 4 for this lambda function and it returns D result 24 without further
down statement because lambda functions automatically evaluate the expression and return the result.
The the lambda function can be used along with built in functions such as map filter and reduce.
So now let's talk about these three functions beginning with the map function the map function in Python
takes in a function and a sequence such as a list as arguments.
So we have a function and then a sequence parsed as arguments to the map function and this function
is applied on each element of the sequence and a map object is done.
So let us see how we can use the lambda function along with the map function.
So we we will define a lambda function which checks if an element from this list is less than or greater
than zero.
The argument to this is X which denotes each element in this list and the argument is separated with
the expression using a colon.
And this is the expression to be evaluated.
So for every element for which x is greater than zero the map function returns a true for this particular
element and then you passed the list as one of the arguments to the map function.
So here you have your map function containing a lambda function as the first argument and the lambda
function checks if each element in the list is greater than 0.
And the second argument is the list itself.
So when you execute this line of code a Map object is written and this map object needs to be passed
to a list constructor or a set constructor to create a list.
Now let us pass this map function to a list constructor like this and then execute this line.
And here we have boolean values returned after checking this condition.
Boolean values return in the form of a list after checking the condition for each element in the list.
Here's another example where we are multiplying each element in the list with two with the number two.
So when you execute this line of code a Map object is done.
Now if you want to create a list you can parse this map function to a list constructor and then execute.
Here you have the list.
The map function can be applied to more than one list.
And in this example I have defined a lambda function which and the element in both the so the lambda
function is applied on board the list so it first applies these function on the element at the 0 8 index.
Then it goes to the element in the first index and then the second and so on in our example the length
of the list L1 and L2 are different.
So one list has less elements than the other.
So in that case in Python 3 the map function will stop after finishing with the shortest list.
That means the map function will stop when the shortest list has been consumed.
So let's look at this example in the Jupiter notebook.
And here I have defined to list L1 and L2 using the map and the lambda functions.
I want to add the element in the list that is first add the element in the 0 8 index.
Then add the elements in the first index and so on and then finally generate a list.
I have parsed this lambda function to the map function as an argument as the first argument.
The second argument is the list L 1 and the third argument is the list L 2.
Now let's execute the code in this cell and then we have the element from list L1 and L2 added.
Now if you'll see the length of the output list is only for that because until the shortest list has
been consumed the map function get executed or the map function will stop when the shortest list has
been consumed.