Javaexercise.com

Lambda Function or Anonymous function in Python

Python is a high-level, interpreted programming language, and is one of the most popular programming languages present in the software industry.

One of the most important but often overlooked concepts in python is that of lambda functions. Let us have a look at them in detail.

What is a lambda function?

In python, a lambda function is used for very simple tasks where it isn’t necessary to explicitly declare a function.

This is when the programmer is more interested in a one-time-use solution rather than defining a function and then calling it.

Lambda functions are also known as anonymous functions. One important thing to remember about lambda functions is that they can take any number of arguments but only one expression is allowed.

Let us have a look at a few characteristics of lambda functions.

Just like any other function in python, the lambda function executes the expression as per the arguments supplied and returns a value.

We can use lambda functions wherever we require function objects.

Syntax

The syntax of representing a lambda function is very straightforward and is as follows.

We simply use the keyword lambda followed by the arguments and the expression separated by a colon:

lambda <arguments> : <expression>

Let us now have a look at a few examples of using the lambda function:

Zero argument lambda function

We can create a lambda function that does not require any argument that means no-args lambda function.

Here, we created a simple lambda to understand the use and syntax of this feature.

# creating a no argument lambda function
fun = lambda : print("Hello")
# calling lambda  function
fun() 

Output:

Hello

One argument lambda function

Like python functions, we can create lambda with single or multiple arguments to pass multiple values during the call.

# creating a no argument lambda function
fun = lambda str : print("Hello",str)
# calling lambda  function
fun("Rohan") 

Output:

Hello Rohan

Two or multiple arguments in lambda function in python

Similarly, we define a lambda function that takes in two arguments x and y and has a simple expression to return the sum x+y of the two arguments.

Then we call it on the next line and pass 2 and 3 as the arguments for the function.

This results in the sum of 2 and 3 that is 5 being printed as an output.

Let us look at the python code and corresponding output for this example:

# Creating a lambda function
func = lambda x,y : x+y

# calling lambda function with two arguments
print(func(2,3))

Output

5

We do not need to necessarily define the lambda function separately and assign it to a variable before calling it.

We can do it in a shortcut manner as well.

Let us look at the python code and corresponding output for this example - 

print((lambda x,y : x+y)(2,3))

Output

5

Getting the sum of  list elements using lambda function  in python

We can use the lambda function to perform any task with a concise code. Here, we are getting sum of the Python list elements.

from functools import reduce
# Take a list
list1 = [2,3,4,6,1,9,4]
print("list ",list1)
# getting sum of list using lambda function
fun = reduce((lambda a,b : a+b),list1)
# calling lambda  function
print("sum = ",fun)

Output

list  [2, 3, 4, 6, 1, 9, 4]
sum =  29

Filtering list elements using lambda function in python

Similarly, we can use it to filter the list elements.

# Take a list
list1 = [2,3,4,6,1,9,4]
print("list ",list1)
# getting filter elememts of list using lambda function
list1 = filter(lambda x: x % 2 == 0, list1)
# calling lambda  function
for num in list1:
    print(num, end=" ")

Output:

list  [2, 3, 4, 6, 1, 9, 4]
2 4 6 4

Let's see some more simple use cases of the lambda expression/function in python.

Taking an argument and printing it using lambda in Python

A lambda function that takes in an argument x and has a simple expression to print the value of it.

Then we call it on the next line and pass 1 as the argument for the function. This results in 1 being printed as an output.

Let us look at the python code and corresponding output for this example:

# Creating lambda function
func = lambda x : print(x)

# calling lambda function
func(1)

Output

1

We do not need to necessarily define the lambda function separately and assign it to a variable before calling it.

We can do it in a shortcut manner as well.

Let us look at the python code and corresponding output for this example:

(lambda x : print(x))(1)

Output

1

Adding 10 to an argument of lambda function in python

We define a lambda function that takes in an argument x and has a simple expression to add 10 to the value of the argument.

Then we call it on the next line and pass 5 as the argument for the function. This results in 15 being printed as an output.

Let us look at the python code and corresponding output for this example:

func = lambda x : x+10

print(func(5))

Output

15

We do not need to necessarily define the lambda function separately and assign it to a variable before calling it. We can do it in a shortcut manner as well.

Let us look at the python code and corresponding output for this example:

print((lambda x : x+10)(5))

Output

15

Conclusion

In this topic, we have learned the use and advantages of the lambda function in a Python program, following a simple running example, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to [email protected] in case of any suggestions