Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Function_Lambda_map_reduce

This has notes about Lambda map reduce function type in python programming

Uploaded by

Swaty
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Function_Lambda_map_reduce

This has notes about Lambda map reduce function type in python programming

Uploaded by

Swaty
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Python Anonymous/Lambda Function

 Anonymous function is a function that is defined without a name.

 While normal functions are defined using the def keyword, anonymous functions are defined
using the lambda keyword.

 Syntax of Lambda Function


 lambda arguments: expression

 Lambda functions can have any number of arguments but only one expression. The expression is
evaluated and returned. The main purpose of anonymous function is just for instant use(i.e for
one time usage)

 Sometimes we can pass function as argument to another function. In such cases lambda
functions are best choice.

 In Python, we generally use it as an argument to a higher-order function.


8/21/2019 Python Programming 53

Dr. D.P.Sharma,MUJ
Python Anonymous/Lambda Function

8/21/2019 Python Programming 54

Dr. D.P.Sharma,MUJ
Python Anonymous/Lambda Function

8/21/2019 Python Programming 55

Dr. D.P.Sharma,MUJ
Python Anonymous/Lambda Function

8/21/2019 Python Programming 56

Dr. D.P.Sharma,MUJ
Python Anonymous/Lambda Function

8/21/2019 Python Programming 57

Dr. D.P.Sharma,MUJ
Python Anonymous/Lambda Function--uses
We can use lambda functions very commonly with filter(), map() and reduce() functions, because
these functions expect function as argument.

Syntax :

map(function, sequence)

filter(function, sequence)

reduce(function, sequence)

8/21/2019 Python Programming 58

Dr. D.P.Sharma,MUJ
Python Map Function

8/21/2019 Python Programming 59

Dr. D.P.Sharma,MUJ
Python Anonymous/Lambda-Map Function

8/21/2019 Python Programming 60

Dr. D.P.Sharma,MUJ
Python Anonymous/Lambda-Map Function

8/21/2019 Python Programming 61

Dr. D.P.Sharma,MUJ
Python Anonymous/Lambda-MapFunction

8/21/2019 Python Programming 62

Dr. D.P.Sharma,MUJ
Python Anonymous/Lambda-Map Function

# Program to double each item in a list using map()

my_list = [1, 5, 4, 6, 8, 11, 3, 12]


new_list = list(map(lambda x: x * 2 , my_list))

# Output: [2, 10, 8, 12, 16, 22, 6, 24]


print(new_list)

8/21/2019 Python Programming 63

Dr. D.P.Sharma,MUJ
Python Anonymous/Lambda-Filter Function
 The filter() function in Python takes two arguments:

 Function
 Sequence as arguments.

 The function is called with all the items in the list and a new list is returned which
contains items for which the function evaluates to True.

8/21/2019 Python Programming 64

Dr. D.P.Sharma,MUJ
Python Anonymous/Lambda-Filter Function

8/21/2019 Python Programming 65

Dr. D.P.Sharma,MUJ
Python Anonymous/Lambda-Filter Function

8/21/2019 Python Programming 66

Dr. D.P.Sharma,MUJ
Python Anonymous/Lambda-Reduce Function

8/21/2019 Python Programming 67

Dr. D.P.Sharma,MUJ
Python Anonymous/Lambda-Reduce Function

8/21/2019 Python Programming 68

Dr. D.P.Sharma,MUJ
Python Anonymous/Lambda-Reduce Function
# python code to demonstrate working of reduce()

# importing functools for reduce()


import functools

# initializing list
lis = [ 1 , 3, 5, 6, 2, ]

# using reduce to compute sum of list


print ("The sum of the list elements is : ",end="")
print (functools.reduce(lambda a,b : a+b,lis))

# using reduce to compute maximum element from list


print ("The maximum element of the list is : ",end="")
print (functools.reduce(lambda a,b : a if a > b else b,lis))

8/21/2019 Python Programming 69

Dr. D.P.Sharma,MUJ

You might also like