1 00:00:00,210 --> 00:00:10,700 Lambda functions by dint allows us to create anonymous functions meaning functions having no names. 2 00:00:10,950 --> 00:00:21,560 Using the facility known as lambda function and lambda functions small functions usually not more than 3 00:00:21,560 --> 00:00:22,400 in line. 4 00:00:22,460 --> 00:00:25,690 And this is the syntax for a lambda function. 5 00:00:25,770 --> 00:00:34,270 Here to have define a normal function and a normal function is defined using the keyword death. 6 00:00:34,280 --> 00:00:44,240 And then we have the name of the function followed by the parameters parsed in parentheses and function 7 00:00:44,240 --> 00:00:51,060 header and the body are separated by a colon and then this particular function. 8 00:00:51,060 --> 00:00:57,160 We are returning the sum of these parameters are the arguments pass to this function. 9 00:00:57,160 --> 00:01:05,370 Now if we want to return the same result using a lambda function here we have the lambda function when 10 00:01:05,370 --> 00:01:14,160 you want to use a lambda function you start with D key word lambda and we know lambda functions do not 11 00:01:14,160 --> 00:01:15,180 have a name. 12 00:01:15,690 --> 00:01:26,430 So next we pass in the argument by separating them with commas and the argument and the function body 13 00:01:26,670 --> 00:01:37,020 are separated by a colon and thus the function body is also known as an expression and this expression 14 00:01:37,050 --> 00:01:42,170 is evaluated by Python and the result put it on. 15 00:01:42,270 --> 00:01:52,240 And when Python executes a lambda function it evaluates this expression and automatically it dawned 16 00:01:52,390 --> 00:01:54,600 it results lambda. 17 00:01:54,640 --> 00:02:04,770 Definition does not include that or don't statement it always contains an expression which is done. 18 00:02:04,930 --> 00:02:08,960 So there's always an implicit return statement. 19 00:02:09,060 --> 00:02:17,740 A lambda function can have any number of arguments unlimited number of arguments like V bars to a normal 20 00:02:17,740 --> 00:02:24,640 function but it should contain only one expression which is evaluated. 21 00:02:24,640 --> 00:02:34,090 So here we have a lambda expression which evaluates the expression explicit why and how do you parse 22 00:02:34,210 --> 00:02:36,390 arguments to a lambda function. 23 00:02:36,400 --> 00:02:45,700 You can parse arguments to a lambda function see if you want to parse the arguments 2 and 3 you can 24 00:02:45,700 --> 00:02:58,590 parse like this and this expression has Red Dawn the result 5 or you can also assign the lambda function 25 00:02:58,590 --> 00:03:02,820 to a variable R and identify you like this. 26 00:03:02,820 --> 00:03:10,320 And if you want to parse arguments to this lambda function you can do this like we do for the normal 27 00:03:10,320 --> 00:03:21,800 function say the argument at 2 3 and 4 for this lambda function and it returns D result 24 without further 28 00:03:21,810 --> 00:03:32,000 down statement because lambda functions automatically evaluate the expression and return the result. 29 00:03:32,240 --> 00:03:42,940 The the lambda function can be used along with built in functions such as map filter and reduce. 30 00:03:42,950 --> 00:03:51,800 So now let's talk about these three functions beginning with the map function the map function in Python 31 00:03:52,040 --> 00:03:58,190 takes in a function and a sequence such as a list as arguments. 32 00:03:58,190 --> 00:04:06,380 So we have a function and then a sequence parsed as arguments to the map function and this function 33 00:04:06,770 --> 00:04:16,770 is applied on each element of the sequence and a map object is done. 34 00:04:17,370 --> 00:04:24,680 So let us see how we can use the lambda function along with the map function. 35 00:04:24,900 --> 00:04:36,230 So we we will define a lambda function which checks if an element from this list is less than or greater 36 00:04:36,230 --> 00:04:37,770 than zero. 37 00:04:37,820 --> 00:04:47,210 The argument to this is X which denotes each element in this list and the argument is separated with 38 00:04:47,210 --> 00:04:50,210 the expression using a colon. 39 00:04:50,210 --> 00:04:53,240 And this is the expression to be evaluated. 40 00:04:54,200 --> 00:05:02,060 So for every element for which x is greater than zero the map function returns a true for this particular 41 00:05:02,180 --> 00:05:11,310 element and then you passed the list as one of the arguments to the map function. 42 00:05:11,310 --> 00:05:18,240 So here you have your map function containing a lambda function as the first argument and the lambda 43 00:05:18,240 --> 00:05:23,690 function checks if each element in the list is greater than 0. 44 00:05:24,150 --> 00:05:27,570 And the second argument is the list itself. 45 00:05:27,570 --> 00:05:36,910 So when you execute this line of code a Map object is written and this map object needs to be passed 46 00:05:36,910 --> 00:05:42,330 to a list constructor or a set constructor to create a list. 47 00:05:42,330 --> 00:05:51,930 Now let us pass this map function to a list constructor like this and then execute this line. 48 00:05:52,000 --> 00:05:59,770 And here we have boolean values returned after checking this condition. 49 00:05:59,770 --> 00:06:06,220 Boolean values return in the form of a list after checking the condition for each element in the list. 50 00:06:06,360 --> 00:06:13,810 Here's another example where we are multiplying each element in the list with two with the number two. 51 00:06:13,930 --> 00:06:18,240 So when you execute this line of code a Map object is done. 52 00:06:18,250 --> 00:06:27,020 Now if you want to create a list you can parse this map function to a list constructor and then execute. 53 00:06:27,040 --> 00:06:30,260 Here you have the list. 54 00:06:30,260 --> 00:06:35,520 The map function can be applied to more than one list. 55 00:06:35,690 --> 00:06:45,740 And in this example I have defined a lambda function which and the element in both the so the lambda 56 00:06:45,740 --> 00:06:55,960 function is applied on board the list so it first applies these function on the element at the 0 8 index. 57 00:06:56,060 --> 00:07:09,750 Then it goes to the element in the first index and then the second and so on in our example the length 58 00:07:09,840 --> 00:07:14,270 of the list L1 and L2 are different. 59 00:07:14,340 --> 00:07:17,970 So one list has less elements than the other. 60 00:07:18,810 --> 00:07:26,840 So in that case in Python 3 the map function will stop after finishing with the shortest list. 61 00:07:26,850 --> 00:07:33,110 That means the map function will stop when the shortest list has been consumed. 62 00:07:33,360 --> 00:07:37,210 So let's look at this example in the Jupiter notebook. 63 00:07:37,320 --> 00:07:44,300 And here I have defined to list L1 and L2 using the map and the lambda functions. 64 00:07:44,310 --> 00:07:51,660 I want to add the element in the list that is first add the element in the 0 8 index. 65 00:07:51,690 --> 00:07:59,100 Then add the elements in the first index and so on and then finally generate a list. 66 00:07:59,220 --> 00:08:06,760 I have parsed this lambda function to the map function as an argument as the first argument. 67 00:08:06,790 --> 00:08:13,810 The second argument is the list L 1 and the third argument is the list L 2. 68 00:08:13,810 --> 00:08:25,190 Now let's execute the code in this cell and then we have the element from list L1 and L2 added. 69 00:08:25,210 --> 00:08:36,040 Now if you'll see the length of the output list is only for that because until the shortest list has 70 00:08:36,040 --> 00:08:44,890 been consumed the map function get executed or the map function will stop when the shortest list has 71 00:08:44,890 --> 00:08:46,060 been consumed.