Lambda | map | filter | reduce function in python | Functional Programming Module in python msbte
Lambda | map | filter | reduce function in python | Functional Programming Module in python msbte |
Functional Programming Module
- Functional Programming is a popular programming paradigm that is closely related to computer sciences mathematical foundations.Actually there is no strict defination of a functional programming but however,we consider them to be language that uses functions to transform data.
- Python is not a functional programming language but it include some of its concepts along with other programmings concepts. With Python, it's easy to write code in a functional style, which may provide the best solution for hand.
- In this section we will discuss four very famous and commonly used functional programming modules.
- lambda function
- map function
- filter function
- reduce function
1. lambda function
- Many Python programmers call this as Anonymous Functions. In general, we use keyword 'def' to define a function and similarly keyword 'lambda' is used to create lambda expressions. To create lambda expression, general syntax is:
- lambda parameters : expression
- A lambda function is a one line function that solves and returns expression result based on provided parameter. (For anyone familiar with C++, lambda function is replacement of inline function.) Let us make one comparison between a function and lambda expression, defined for obtaining cube of parameter.
def get_cube(num): return num*num*num print get cube(5)) #prints 125
- A user-defined function get_cubel) is defined (line-1 and line-2) with one parameter 'num' that returns cube of 'num'. Line-3 calls get cube() with parameter 5 and prints. For the same, lambda expression can be written as:
res = lambda num : num*num*num print(res(5)) #prints 125
- Lambda definition does not contain a "return" statement because it contains an expression which is always return. We can also use a lambda function definition anywhere a function is there, and we don't have to assign it to a variable . This is the simplicity of lambda functions. A lambda statement can have any numbers of parameters.
- Following are simple single lambda expression statements with output.
- Example :-
addition = lambda x,y : x + y print("Addition of 2 and 5 is :",addition(2,5))
- Will result as :
Addition of 2 and 5 is : 7
2. map function
- To begin our discussion with map() function, first refer following program where we are printing square of each element of a list.
def squares(n): return (n*n) list1 =[1,2,3,4,5] for i in list1: s=squares(i) print(s)
- This code will print squares of each list element. Note that, each element of list are iteratively passed to squares) function in a loop. Now, the map() function in Python takes in a function and a sequence as argument, and allows us to apply a function to every element provided sequence. Let's change the same program and apply map! ) to call squares() function.
def sq(n): return (n*n) list1 =[1,2,3,4,5] res = map(sq,list1) #Don't use parenthesis while specifying function name as parameter print("After mapping sq() with listl :", list(res))
- This will give following output:
- Note that, map() function maps/joins/collaborates between specified function and list elements. We can also use lambda expression instead of defining a function and calling it. Here is the same example containing lambda expression instead of function.
lis =[1,2,3,4,5] sq = list(map(lambda n: n*n ,lis)) print("lambda with map in lis =", sq)
- This code result as :
After mapping sq with listl : [1, 4, 9, 16, 25]
lambda function with map in listl : [1, 4, 9, 16, 25]
3. Filter function
- Similar to map() function, the filter() function also takes in a function name and a list as arguments. This offers an easyiest technique to filter all the elements of list given, for which the function returns True. Here program that return all odd numbers from an list given:
def odd_elements(num): if (num % 2 != 0): return True list1 = [5,7,22,97,54,62,77,23,73,61] res_list=list(filter(odd_elements, list1)) print(res_list)
- Note that, line-5 calls to odd_elements() function and passes elements of list1, one-by-one and generates resulting list of elements only for which the odd_elements( ) returned True. Hence, we will get following output:
[5,7,97,77,23,73,61]
list1 = [5,7,22,97,54,62,77,23,73,61] res_list=list(filter(lambda x:(x % 2 != 0), list1)) print(res_list)
4. reduce function
- The built-in module functools provides reduce() function. The reduce( ) function also takes a function name and a list as argument. The function is called with lambda function ,a list and a reduced single result is return. This performs a continuing operation on the elements of list.
from functools import reduce list1 = [41,11,42,13,17] res= reduce(lambda x,y : x + y,list1) print(res)
- Let us discuss the working of reduce() function. The lambda expression is having two parameters, therefore first two parameters are passed to lambda expression (i.e. 47 and 11) and their result is added to the third element (i.e. 58 + 42) and their result to the fourth element (i.e. 100 + 13), and this goes on till the end of the list. Following is the code that results same, but includes user-defined function instead of lambda expression. It will also work on pair of two parameters as explained for exact previous example.
from functools import reduce def addition(x,y): return x + y list1 = [41,11,42,13,17] res= reduce(addition,list1) print(res)
Check :- Python Modules | Creating Modules and Importing Modules In Python MSBTE
Post a Comment