Lambda, Filter and Reduce

Created at 2016-01-01 Updated at 2017-03-05 Category experience / work Tag python / 3 / filter / Lambda / python3 / reduce

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

lambda

 

Filter

Affective-FilterThe 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

filter

 

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

reduce

 

All the above example are of Python 3.

 

Site by Ashutosh Kumar Singh using Hexo & Random

Traveller - Developer - Foodie - Biker - Cyclist - Rider - Technocrat