Lambda, Filter and Reduce
Created at 2016-01-01 Updated at 2023-03-08 Category experience / work
The lambda operator or lambda function is a way to create small anonymous functions, i.e. without a name. These functions are throw-away functions, i.e. they are just needed where they have been created. Lambda functions are mainly used in combination with the filter(), map() and reduce().
lambda argument_list: expression
The function filter(f,l) needs a function f as its first argument. f has to return a Boolean value, i.e. either True or False. This function will be applied to every element of the list _l_. Only if f returns True will the element be produced by the iterator, which is the return value of filter(function, sequence).
The function reduce(func, seq) continually applies the function func() to the sequence seq. It returns a single value. It actually first apply the function to first two element. Then there result would be applies to the third element. It will continue until all element have not been applied to that function.
All the above example are of Python 3.