The reduced function in Python takes in a function and a list as arguments and the function is called.
With a lambda function like this and a replicator operation is performed or what D pairs of the list.
So as per the lambda expression the argument is X and Y denote the element that the 0 it and the first
index and the expression as per the expression.
We need to add these elements at the 0 8 and the first index.
So as a first step we apply the function on these two elements and an intermediate result is stored.
So two plus three is five and in the next step the same function is applied on the intermediate result.
And the element at the second index and the intermediate result is again stored.
So this time five plus four is nine and this intermediate results stored again and in the third step.
The function is applied on the intermediate result 9 and the element at the index number three and the
intermediate result is stored again.
And this process continues then no more elements are left in the list and the final result is returned.
And remember that the function reduce stores the intermediate result and only to return to the final
summation value.
The reduced function is not commonly used in Python so it was removed from the building functions and
python but it is still available in the function tools.
Module in order to use the reduced function we have to import the function tools.
This line in the cell that you see is used to import the reduced function from func tools import reduce
now coming to what example I have defined a list L1 and using the reduced function.
I want to calculate the sum of all the elements in this list.
So I'm going to use a lambda function to calculate the sum of the elements and the arguments pass to
the reduced function.
The lambda function and then the list.
List 1.
So let's execute this one to execute this.
You get the sum of all of these elements in the list
and another example that we want to calculate.
The element would be with the maximum value in this list.
List too.
So using the function that you use and then the lambda function I have past two arguments x and y x
and y denote the elements in the list.
So and in the expression we have a condition that in X and Y are compared and if x is greater than y
the element x is written and the element Y is written and this expression is used to return the maximum
value in the list.
So let's execute this.
And here we have the output.
Now let's talk about the filter function.
The filter function takes in a function and a list as the arguments and it applies this function on
each of the elements in the list and the function.
This particular function checks if the condition is true for each element in the list.
Only if it is true if the condition turns out to be true only then the element is included in the output
list.
So let's see an example to understand the filter function.
So here we have a filter function which includes a lambda function the lambda function checks if each
element in the list is divisible by 2 or not.
If the remainder is 0 or not and when we execute this the code in the cell we get an object.
And if you want to create a list then you can parse this object to the list constructor and then execute
and you have the elements which are divisible by two so here we have a map function that includes a
lambda function and then and I a bill that is a list so the lambda function takes the argument X which
indicate each element in the list and the expression here checks of each element is either greater or
less than zero.
If it is greater than zero then the function returns the Boolean value true.
Is it returns false if the value of the element is greater than zero then it returns through.
And if it is less than zero it returns false.
And this condition is checked for all the elements in the list.
So let's execute this code in the Jupiter notebook.