Python Lambda / Python Anonymous



What are Lambda Functions in Python?

In Python, a lambda function is a function that is defined without a name.

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

A lambda function can take any number of arguments, but can only have one expression.

Lambda functions are also called anonymous functions.


Syntax of Lambda Function in Python

In Python, a lambda function has the following syntax.

lambda arguments: expression

Lambda functions can take any number of arguments but only one expression.

The expression in a lambda function is evaluated and returned.

Lambda functions can be used wherever function objects are expected.


Example of Lambda Function in Python

In the following example, we will define a lambda function that adds 5 to the input value.

result = lambda x: x + 5

print(result(4))
print(result(2))

Output

9
7

In the above example, lambda x: x + 5 is the lambda function. The x is the argument, and x + 5 is the expression that gets evaluated and returned.

The lambda function lambda x: x + 5 has no name. It returns a function object which is assigned to the identifier result. We can now call it as a normal function.

result = lambda x: x + 5

The above statement is the same as:

def result(x):
    return x + 5

How to use Lambda Functions in Python?

Lambda functions are used when an anonymous function is required for a short period of time.

In Python, we generally use a lambda function as an argument to a higher-order function (a function that takes in other function as arguments).

Lambda functions are used along with built-in functions like map(), filter() etc.


Example of a Lambda Function with map()

The map() function takes in a function and a list as arguments.

When using a map() function, the passed lambda function is applied on each item in the given list, and a new list is returned containing items after the lambda function transformation.

In the following example, we will use the map() function to add 5 for each item in a list.

# program to add 5 for each item in a list using the `map()` function

my_list = [0, 3, 4, 6, 8, 9, 12]

new_list = list(map(lambda x: x + 5, my_list))

print(new_list)

Output

[5, 8, 9, 11, 13, 14, 17]

Example of a Lambda Function with filter()

The filter() function takes in a function and a list as arguments.

When using a filter() function, the passed lambda function is applied on each item in the given list, and a new list is returned containing items for which the lambda function evaluates to True.

In the following example, we will use the filter() function to filter out only odd numbers from a list.

# Program to filter out only the odd numbers from a list using the filter() function

my_list = [3, 6, 9, 8, 12, 11, 7, 24, 16, 17]

new_list = list(filter(lambda x: (x%2 != 0), my_list))

print(new_list)

Output

[3, 9, 11, 7, 17]


ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.