27.Python Anonymous/Lambda Function

In Python, an anonymous function is a function that is defined without a name.These functions are called anonymous because they are not declared in the standard manner by using the def keyword. You can use the lambda keyword to create small anonymous functions.

The Anonymous Functions
Lambda forms can take any number of arguments but return just one value in the form of an expression. They cannot contain commands or multiple expressions.
An anonymous function cannot be a direct call to print because lambda requires an expression.
Lambda functions have their own local namespace and cannot access variables other than those in their parameter list and those in the global namespace.
Although it appears that lambda's are a one-line version of a function, they are not equivalent to inline statements in C or C++, whose purpose is by passing function stack allocation during invocation for performance reasons.

Syntax of Lambda Function in python
lambda [arg1 [,arg2,.....argn]]:expression

Lambda functions can have any number of arguments but only one expression. The expression is evaluated and returned. Lambda functions can be used wherever function objects are required.

Example of Lambda Function in python

Here is an example of lambda function that doubles the input value.
# Program to show the use of lambda functions 
double = lambda x: x * 2
print(double(5))

Output
10

In the above program, lambda x: x * 2 is the lambda function. Here x is the argument and x * 2 is the expression that gets evaluated and returned.

This function has no name. It returns a function object which is assigned to the identifier double. We can now call it as a normal function. The statement 

double = lambda x: x * 2
is nearly the same as:
def double(x): 
     return x * 2

let f(x,y)=2xy+3x+2 we can define a lambda function as follows
>>f=lambda x,y: 2*x*y+3*x +2
>>f(2,3)
20

Use of Lambda Function in python

We use lambda functions when we require a nameless function for a short period of time.

In Python, we generally use it as an argument to a higher-order function (a function that takes in other functions as arguments). Lambda functions are used along with built-in functions like filter(), map() etc.

Example use with filter()
The filter() function in Python takes in a function and a list 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.
Here is an example use of filter() function to filter out only even numbers from a list.

# Program to filter out only the even items from a list 
my_list = [1, 5, 4, 6, 8, 11, 3, 12] 
new_list = list(filter(lambda x: (x%2 == 0) , my_list))
print(new_list)

Output[4, 6, 8, 12]

Use higher order python function filter to extract a list of positive numbers from a given list of numbers. You should use a lambda to create the auxiliary function. ( University Question)
# Python program to print positive Numbers in a List

# list of numbers
list1 = [-10, 21, 4, -45, -66, 93, -11]
pos_nos = list(filter(lambda x: (x >= 0), list1))
print("Positive numbers in the list: ", *pos_nos)

Example use with map()

The map() function in Python takes in a function and a list.

The function is called with all the items in the list and a new list is returned which contains items returned by that function for each item.

Here is an example use of map() function to double all the items in a list.
# 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)) 
 print(new_list)

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

Comments

Popular posts from this blog

Python For Machine Learning - CST 283 - KTU Minor Notes- Dr Binu V P

46.Classes and Objects in Python- Accessors and mutators

KTU Python for machine learning Sample Question Paper and Answer Key Dec 2020