How to use Lambda Function in Python?

The anonymous nature of Python Lambda Functions indicates that they lack a name. As we already know, a standard Python function is defined using the def keyword. Similar to this, the lambda keyword is utilized in Python to declare an anonymous function.


The syntax of the lambda expression in python is as follows −

lambda arguments: expression

There is only one expression that is evaluated and returned in this function, regardless of the number of parameters.

Lambda functions can be used anywhere that function objects are required.

The fact that lambda functions are syntactically limited to a single expression must always be kept in mind. In addition to other kinds of expressions in functions, it has a variety of purposes in certain areas of programming. They return the definition of the function on the fly.

Lambda functions don't contain a return statement, they always return an expression. You can always put a lambda definition anywhere a function is expected. Suppose we have a function which is to be used only once and called from only one place, then we can use lambda functions. So you don't need to give it a name and you can define the functionality there itself. Hence, we eliminate the use of a function and use Lambda expression.

Example 1

Let’s look at an lambda function in the following example.

# Python program to demonstrate lambda functions x ="HelloWorld" # lambda gets pass to print (lambda x : print(x))(x)


The following output is obtained.


Example 2

Let’s look at example used for adding numbers using lambda function. In the following example we give an int value as a single argument to the lambda function which adds 100 to the given number.

x = lambda a : a + 100 print(x(5))


The following output is obtained.


Example 3

As seen in the above example where we gave the lambda function a single argument as input, we can give multiple arguments as input. Let’s look an example with multiple arguments.

x = lambda a, b : a * b print(x(9, 1))


The following output is obtained.


Example 4

Let’s look at a lambda function used for list comprehension.

Lists are used to store multiple items in a single variable. One of the four built-in data types in Python for storing data collections is the list; the other three are the tuple, set, and dictionary, each of which has a unique purpose. When you wish to make a new list based on the values of an existing list, list comprehension offers a more concise syntax.

tables = [lambda x=x: x*10 for x in range(1, 11)] for table in tables: print(table())


The following is the output obtained.


Example 5

Although multiple statements are not permitted in lambda functions, we can create two lambda functions and then call one of them as a parameter to another. Let's use lambda to search for the second greatest element.

List = [[4,3,4],[9, 4, 16, 64],[3, 6, 9, 12]] #Sorting sublist sortList = lambda x: (sorted(i) for i in x) # Get the second largest element secondLargest = lambda x, f : [y[len(y)-2] for y in f(x)] result = secondLargest(List, sortList) print(result)


The output is as follows.

[4, 16, 9]