Python Lambda

Python Lambda Function

Python Lambda function is a single line function whose body is just an expression and can accept multiple arguments.

In this tutorial, we shall first learn the basics of lambda function and secondly some interesting hacks that we can do with these lambda functions.

Syntax of Lambda Function

Following is the syntax of Lambda Function.

lambda arguments : expression

You can assign this lambda function to a variable. Something like in below code snippet.

x = lambda arguments : expression

And you can call the lambda function with the variable name x. Following is an example code snippet to call the lambda function.

result = x(arguments)

The arguments are passed just like a regular function’s arguments, but the function name being the variable we assigned this lambda function to.

When you call a lambda function, the expression is evaluated and the evaluated value is returned. So, you have the arguments, body and return statement, everything in just a line. So very concise.

Example 1: Python Lambda Function

Following is a simple lambda function, that takes a number and returns the square of it.

Python Program

#lambda function
square = lambda a: a*a
#call lambda function
result = square(6)
print(result)
Run this program

Output

36

Example 2: Python Lambda Function – Multiple arguments

We already mentioned that a Python Lambda function can accept multiple arguments. In the following example, we define a lambda function with two arguments, say a and b, and we shall multiplication of a and b.

Python Program

#lambda function
mul = lambda a,b: a*b
#call lambda function
result = mul(5,3)
print(result)
Run this program

Output

15

Example 3: Python Lambda Function with No Arguments

Yeah! You can define a lambda function with no arguments at all. If there were to be requirement that you create a lambda function with no arguments, you can.

In the following example, we define a lambda function with no arguments, and returns 6 when called.

Python Program

#lambda function
six = lambda : 6
#call lambda function
result = six()
print(result)
Run this program

Output

6

Recursive Lambda Function

You can design a recursion function using lambda function. According to the syntax of Python Lambda function, all that you need is an expression that evaluates to a single value to return. So, if you can build a recursive function satisfying this criteria, you have a recursive lambda function.

In the following example, we defined a lambda function that can calculate the factorial of a given number, using recursion.

Python Program

#recursive lambda function
factorial = lambda a: a*factorial(a-1) if (a>1) else 1
#call lambda function
result = factorial(5)
print(result)
Run this program

Output

120

Return Lambda Function

Lambda Function is just like a variable. So, you can return a lambda function from another function.

Python Program

import math
#function returning lambda function
def myfunc(n):
  return lambda a : math.pow(a, n)

#lambda functions
square = myfunc(2) #square = lambda a : math.pow(a, 2)
cube = myfunc(3) #cube =  = lambda a : math.pow(a, 3)
squareroot = myfunc(0.5) #squareroot = lambda a : math.pow(a, 0.5) 

print(square(3))
print(cube(3))
print(squareroot(3))
Run this program

Output

9.0
27.0
1.7320508075688772

Summary

Concluding this tutorial of Python Examples, we learned how to define a lambda function, and use it in your program constructively, with the help of example programs.