Functional Programming in Python 🐍
Posted on 3 Jul, 2019
Features like lambda
, map
, filter
, reduce
are generally used to perform functional programming related tasks in Python. Let's take a quick look on them.
Lambdas
Anonymous functions
No function name,
They can be passed as function arguments/objects.
No
return
statement, evaluated expression is returned automatically.Single line function.
Example :
Map
applies a function to all the items in an input list.
map(function_to_apply, list_of_inputs)
.
Example :
Filter
creates a list of elements for which a function returns
True
.
Example :
Reduce
accepts a function and a sequence(list/set etc) and returns a single value calculated.
Initially, the function is called with the first two items from the sequence and the result is returned.
The function is then called again with the result obtained in step 1 and the next value in the sequence. This process keeps repeating until there are items in the sequence.
Example :
Last updated