Skip to main content

How To Use Lambda Function in Python

In our previous Python tutorial, we have explained about file hadling in python. In this tutorial, we will explain about Lambda Function in Python.

Lambda functions are similar to user defined function but without name. Due to this, these functions are called anonymous functions. The lambda keyword is used to define an anonymous function in Python.

The Lambda functions are useful we want to use the function once or want to create a function that only contain simple expression or single line of statement.

So let’s play with Lambda function:

How to Define a Lambda Function

The lamda function is defined using lambda keyboard with syntax like this:

lambda argument(s) : expression

In above, the lamda keyboard used to define anonymous functions. The argument(s) is a placeholder used to hold the value passed in the function expression. The expression is the code to execute in the lambda function.

As stated earlier, the lambda functions are anonoymous functions. The anonymous function does not have a return keyword. It will automatically return the result of the expression in the function once it is executed.

Now we will look at an example how lambda function works. We will compare it with user defined functions.

Suppose we want to define a function that multiply number and return result. We define function like below.

def calculate(x):
  return x * 3

print (calculate(5))

We can get same result by using lambda function like this:

x = lambda x: x * 3

print (x(5))

In above code, the lambda function is used which does not have a return keyword. It returns the result of the expression on it’s own. The returned x also serves as a placeholder for the value to be passed into the expression.

We can also use different approaches with lambda function call. Now we will use immediate invoke approch with lambda function call. It will look like below.

x = (lambda x : x * 3)(5)

print (x)

As lambda function is anonoymous (does not have name to invoke), we need to enclose the entire statement to invoke it immediately.

When Use Lambda Function?

Use of lambda functions in Python tend to be the subject of controversies. As there are arguement against lambdas with issues of readablity and heavy syntax with the lambda keyword. Despite these debates, lambda functions have properties that sometimes provide value to the Python language and to it’s developers.

We can use the lambda function to create simple expressions such as expressions that do not include complex structures such as if-else, for-loops, and so on.

The real power of lambda function is to use as an anonymous function inside another user defined function. We will discuss this later with an example.

Some Practical Uses of Lambda Function

The lambda functions are used extensively used with the built-in Python functions such as map() and filter() to handle iterables.

For example, here we are using map() function with lambda function to convert string list to upper case:

name = list(map(lambda x: x.upper(), ['andy', 'smith', 'william']))

print (name)

we can also use filter() function with lambda function to get desired result:

result = list(filter(lambda x: 'a' in x, ['andy', 'smith', 'andre']))

print (result)

As stated earlier, the power of lambda function lies in to use as an anonymous function inside another user defined function.

def multiply(x):
  return lambda y : y * x

doubleMultiply = multiply(2)

print (result(7)) #14

In above example, using function definition to make a function that always double multiple the number we pass.

We can also use the lambda function to used the same function definition to make both functions, in the same program:

def multiply(x):
  return lambda y : y * x

doubleMultiply = multiply(2)
tripleMultiply = multiply(3)

print (doubleMultiply(7))  #14
print (tripleMultiply(7))  #21

Conclusion

In this tutorial, we have explained the basics of the lambda function and how you can commonly use it. Thank you for taking your time to read this tutorial.