Lambda, Filter and Reduce
Created at 2016-01-01 Updated at 2024-11-07 - 2 min. read Category experience / work
Lambda
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().
Syntax
lambda argument_list: expression
Example
Filter
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).
Syntax
filter(function, sequence)
Example
Reduce
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.
Syntax
reduce(func, seq)
Example
All the above example are of Python 3.