1 00:00:00,090 --> 00:00:10,530 The reduced function in Python takes in a function and a list as arguments and the function is called. 2 00:00:10,530 --> 00:00:22,390 With a lambda function like this and a replicator operation is performed or what D pairs of the list. 3 00:00:22,430 --> 00:00:31,780 So as per the lambda expression the argument is X and Y denote the element that the 0 it and the first 4 00:00:31,900 --> 00:00:36,010 index and the expression as per the expression. 5 00:00:36,010 --> 00:00:41,110 We need to add these elements at the 0 8 and the first index. 6 00:00:41,110 --> 00:00:49,720 So as a first step we apply the function on these two elements and an intermediate result is stored. 7 00:00:49,720 --> 00:00:57,760 So two plus three is five and in the next step the same function is applied on the intermediate result. 8 00:00:58,090 --> 00:01:04,810 And the element at the second index and the intermediate result is again stored. 9 00:01:04,840 --> 00:01:12,070 So this time five plus four is nine and this intermediate results stored again and in the third step. 10 00:01:13,310 --> 00:01:23,460 The function is applied on the intermediate result 9 and the element at the index number three and the 11 00:01:23,460 --> 00:01:27,070 intermediate result is stored again. 12 00:01:27,120 --> 00:01:37,440 And this process continues then no more elements are left in the list and the final result is returned. 13 00:01:37,620 --> 00:01:45,870 And remember that the function reduce stores the intermediate result and only to return to the final 14 00:01:45,960 --> 00:01:47,400 summation value. 15 00:01:49,060 --> 00:01:57,460 The reduced function is not commonly used in Python so it was removed from the building functions and 16 00:01:57,460 --> 00:02:02,560 python but it is still available in the function tools. 17 00:02:02,560 --> 00:02:11,540 Module in order to use the reduced function we have to import the function tools. 18 00:02:11,560 --> 00:02:21,160 This line in the cell that you see is used to import the reduced function from func tools import reduce 19 00:02:22,370 --> 00:02:30,200 now coming to what example I have defined a list L1 and using the reduced function. 20 00:02:30,200 --> 00:02:34,820 I want to calculate the sum of all the elements in this list. 21 00:02:34,820 --> 00:02:41,600 So I'm going to use a lambda function to calculate the sum of the elements and the arguments pass to 22 00:02:41,600 --> 00:02:43,480 the reduced function. 23 00:02:43,550 --> 00:02:47,000 The lambda function and then the list. 24 00:02:47,180 --> 00:02:48,540 List 1. 25 00:02:48,560 --> 00:02:51,200 So let's execute this one to execute this. 26 00:02:51,200 --> 00:02:55,040 You get the sum of all of these elements in the list 27 00:02:57,880 --> 00:03:01,090 and another example that we want to calculate. 28 00:03:01,090 --> 00:03:04,940 The element would be with the maximum value in this list. 29 00:03:04,960 --> 00:03:06,240 List too. 30 00:03:06,280 --> 00:03:15,340 So using the function that you use and then the lambda function I have past two arguments x and y x 31 00:03:15,400 --> 00:03:19,740 and y denote the elements in the list. 32 00:03:19,930 --> 00:03:32,330 So and in the expression we have a condition that in X and Y are compared and if x is greater than y 33 00:03:33,050 --> 00:03:42,290 the element x is written and the element Y is written and this expression is used to return the maximum 34 00:03:42,290 --> 00:03:44,270 value in the list. 35 00:03:44,270 --> 00:03:46,580 So let's execute this. 36 00:03:46,760 --> 00:03:51,880 And here we have the output. 37 00:03:51,890 --> 00:03:54,530 Now let's talk about the filter function. 38 00:03:54,680 --> 00:04:07,690 The filter function takes in a function and a list as the arguments and it applies this function on 39 00:04:07,810 --> 00:04:14,580 each of the elements in the list and the function. 40 00:04:14,580 --> 00:04:23,520 This particular function checks if the condition is true for each element in the list. 41 00:04:23,640 --> 00:04:32,830 Only if it is true if the condition turns out to be true only then the element is included in the output 42 00:04:32,830 --> 00:04:33,720 list. 43 00:04:33,820 --> 00:04:36,910 So let's see an example to understand the filter function. 44 00:04:37,690 --> 00:04:45,820 So here we have a filter function which includes a lambda function the lambda function checks if each 45 00:04:45,940 --> 00:04:51,690 element in the list is divisible by 2 or not. 46 00:04:51,700 --> 00:05:01,320 If the remainder is 0 or not and when we execute this the code in the cell we get an object. 47 00:05:01,460 --> 00:05:11,810 And if you want to create a list then you can parse this object to the list constructor and then execute 48 00:05:12,200 --> 00:05:21,390 and you have the elements which are divisible by two so here we have a map function that includes a 49 00:05:21,390 --> 00:05:31,560 lambda function and then and I a bill that is a list so the lambda function takes the argument X which 50 00:05:31,620 --> 00:05:41,010 indicate each element in the list and the expression here checks of each element is either greater or 51 00:05:41,010 --> 00:05:42,660 less than zero. 52 00:05:42,660 --> 00:05:51,130 If it is greater than zero then the function returns the Boolean value true. 53 00:05:51,190 --> 00:05:58,760 Is it returns false if the value of the element is greater than zero then it returns through. 54 00:05:58,870 --> 00:06:02,370 And if it is less than zero it returns false. 55 00:06:02,500 --> 00:06:06,600 And this condition is checked for all the elements in the list. 56 00:06:07,180 --> 00:06:10,840 So let's execute this code in the Jupiter notebook.