Example HTML page

# Lambda functions in Python

Lambda functions

The concept of lambda functions/anonymous functions is one of the interesting topics in Python. It seems like it’s the tough topic but you feel easy during the practice.

The functions which are defined anonymously (without name) are called as anonymous functions. Here we use a keyword ‘lambda’ .Therefore anonymous functions are otherwise called as lambda functions.

The only difference between normal functions and anonymous functions is keywords (def and lambda). We define normal functions by using a keyword “def” keyword whereas anonymous functions are defined using the lambda keyword.

Syntax of lambda function is:

 lambda [args1,args2…]: expression

In Python, wherever you place lambda expression it returns the value of the expression.

Ex

 >>> add=lambda a:a+2                      #Lambda function performing addition >>> print(add(10)) 12 >>> sub=lambda b:b-4                       #Lambda function performing subtraction >>> print(sub(6)) 2 >>> mul=lambda c:c*6                      #Lambda function performing multiplication >>> print(mul(3)) 18 >>> div=lambda d:d/7                       #Lambda function performing division >>> print(div(42)) 6.0 >>> >>> sub=lambda b:b-4 >>> print(add(6)) 8 ###Here the resultant is 8 because the expression lambda b:b-4 returns 2 and the statement print(add(6)) adds that resultant with 6 and returns the output 8.

Consider one of the above functions is ‘add=lambda a:a+2’. Here ‘a’ is an argument and a+2 is the expression which gets evaluated and returned.

The statement ‘add=lambda a:a+2’ is approximately similar as follows:

```>>> def add(a):
return a+2

```

Declaration of lambda functions:

We can declare the lambda functions by the following ways.

Lambdas function with no parameters.

 >>> val=lambda:2 >>> val( ) 2 >>> >>> fun=lambda: True >>> fun( ) True >>> >>> func=lambda: False >>> func( ) False >>> >>> fun=lambda: print(“hello sudhakar”) >>> fun( ) hello sudhakar

Lambdas function with one parameter.

Ex

 >>> x=lambda a:a+2 >>> print(x(5)) 7 >>> >>> f=lambda b:b*b >>> f(8) 64

Lambdas function with two parameters

Ex

 >>> func=lambda s,t:s*t >>> func(3,6) 18

Lambdas function with three parameters

Ex

 >>> L=lambda p,q,r:p+q-r >>> L(3,4,5) 2

Passing string as an argument to the lambda function

We can also pass a string as an argument to the lambda function and is shown in below:

 >>> L_fun1=lambda s:print(s) >>> L_fun1(“Hello”) Hello

Lambdas with Built-in functions

We can use lambda functions with built-in functions such as filter ( ) and map( ).

1.filter( )

The filter() function in Python takes in a function and a list as arguments. When the function evaluates to true it returns the new list otherwise not.

Ex: In the below example it takes all the items in the list. When we use filter( ) function it filters all the items which are divisible by ‘3’ and displays that newly generated list.

 #An example lambda function that prints the multiples of ‘3’. >>> List=[1,2,3,4,5,6,7,8,9,10] >>> List1=list(filter(lambda x:(x%3==0),List)) >>> print(List1) [3, 6, 9] >>> #If the function evaluates to false it prints nothing. We have shown this for the above example. >>> L=[2,7,8,10] >>> L1=list(filter(lambda x:(x%3==0),L)) >>> print(L1) [ ]

2.map( )

It is similar to filter( ) function but instead of printing the resultant elements it prints true or false i.e map ( ) returns values of the expression for each element in the list. For the above same example we applied map( ) function.

 >>> List=[1,2,3,4,5,6,7,8,9,10] >>> List1=list(map(lambda x:(x%3==0),List)) >>> print(List1) [False, False, True, False, False, True, False, False, True, False] >>> >>> L=[2,7,8,10] >>> L1=list(map(lambda x:(x%3==0),L)) >>> print(L1) [False, False, False, False]

The following example differentiates the way of using the normal functions and lambda functions.

#To find the highest number among two numbers using lambda functions.

 >>> highest=lambda x,y:print(“x is highest”) if x>y else print(“y is highest”) >>> highest(12.354,123.54) y is highest

# The same above program can be written by using functions and is as follows:

```>>> def highest(x,y):
if x>y:
print("x is highest")
else:
print("y is highest")

>>> highest(12.354,123.54)
y is highest

```

Example programs

1.A Python program which prints 4 powers of the numbers in the list by using map( ) function.

 >>> d=1,2,3,4,5 >>> list(map(lambda p:p**4,d)) [1, 16, 81, 256, 625]

2.A Python program that extracts only the values which are divisible by ‘10’ from the list by using filter( ) function.

 >>> N=[110,25,40,20,80,60,456] >>> list(filter(lambda c:c%10==0,N)) [110, 40, 20, 80, 60]

Example HTML page