Anonymous/Lambda Function in Python

Lambda keyword is used to declare an Anonymous function, i.e. a function that does not have any name. It is also called Anonymous functions. In python, normal functions are defined using the def keyword, but the lambda keyword is used for defining the Anonymous function.

Syntax of Lambda Function

`lambda Arguments: expression`

• A lambda function can have any number of arguments.
• A lambda function can have only one expression.
• When function objects are required, a lambda function can be used.

Suppose you want to evaluate a function 3x+1 in python using function. One approach is declaring a standard function using the def keyword.

Let’s call it f that have a single parameter x. that will return value 3*x+1

``````def f(x):
return 3 * x + 1
# if we input 1, we will get the value 4.
print(f(1))
``````

Output:

`4`

Let’s do this using anonymous function

``````g = lambda x: 3*x+1
print(g(1))
``````

Output:

`4`

Examples

1. lambda function with 0 argument:
``````show = lambda : “javatpoint”
print(show())
``````

Output

`javatpoint`
• lambda function with single argument:-
``````square = lambda x : x * x
print(square(5))
``````

Output

`25`
• lambda function with multiple arguments:-
``````multiply = lambda x, y: x * y
print(multiply(3,5))
``````

Output

`15`

Need for Lambda Functions

Below are some points for why we need the lambda function, such as:

• These functions are used when we need a function for a short time and namelessly, and only one expression is required.
• Lambda functions are used when others need a function inside their body for a short time.
• Lambda function can use where any function requires another function as a parameter.

Use of Lambda Function inside another Function

Let's say we need a function that returns a value true when a number is divisible by an unknown number so that the lambda function can be used.

``````# function to check if the number is divisible by a number n

def is_divisible(n):
return lambda a: a % n == 0

divisible_with_two = is_divisible(2)
print(divisible_with_two(15))

divisible_with_five = is_divisible(5)
print(divisible_with_five(15))
``````

Output

``````False
True
``````

Use of Lambda Function with Some Built-in Python Functions

1. Use of lambda function with filter()

The filter is a built-in function in python. It takes 2 parameters one is a function that returns a Boolean value, and another is iterable. It returns a new iterable, which contains the items that are evaluated true by the lambda function.
``````primary_list = [5, 6, 29, 34, 8, 21, 9]
# it is a primary list contains some items

new_list = list(filter(lambda x: x>10, primary_list))
# this list contain items of primary list which are grater than 10

print(new_list)
``````

Output

`[29, 34, 21]`
• Use of lambda function with map()

The map is also a built-in function in python. It takes 2 parameters one is a function, and another is iterable. It returns a new iterable, which contains the items that are returned by the lambda function.
``````primary_list = [10, 45, 35, 70, 25, 40, 50]
# it is a primary list contains some items

new_list = list(map(lambda x: x // 5, primary_list))
# this list contains the items which we get after divide each item of pri-mary_list by 5
print(new_list)
``````

Output

`[2, 9, 7, 14, 5, 8, 10]`
• Use of lambda function with reduce()
Reduce is also a built-in function in python. It takes 2 parameters one is a function, and another is iterable. It returns a result that we get after calling the lambda function for each pair. To use reduce function, we need to import functools.
``````from functools import reduce
primary_list = [3, 6, 8, 2, 9, 1]
# it is a primary list contains some items
multiplication = reduce(lambda x, y : x*y, primary_list)
print(multiplication)

``````

Output

`2592`

Difference between Lambda Function and Normal Function

Here are some differences between lambda function and normal function, such as:

• The Lambda function can only have a single expression, while a normal function can have multiple expressions in its body.
• Normal functions are assigned a name, while the lambda function doesn’t have any name.
• We need to write a return statement in the normal function to return a value, while no return statement is needed in the lambda function.

NOTE: Internal working of lambda function and normal function is same.