
Lambda Functions
A lambda function in Python is a small anonymous function defined using the lambda
keyword. It is a way to create small, unnamed functions on the fly without the need for a proper function definition. Lambda functions are useful when you need a simple function for a short period and don't want to define a separate named function.
Syntax of lambda function
# Syntax of defining a lambda functionlambda arguments: expression
Here, arguments are the input parameters of the function, and expression is the action of the function.
Example
Previously we've defined an add
function to calculate the sum of two numbers as follows:
# Define a formal function with "def"def add(a, b): result = a + b return result
Now, let's create a lambda function to achieve the same functionality.
# Define a lambda functionadd_lam = lambda a, b: a + badd_lam(3, 5)
Output:
8
a
and v
are parameters of the lambda function, and they are placed right after the "lambda" keyword. After the colon ":", it's the expression that the lambda function computes and returns.
Achieving the same functionality often requires only a single line of code using lambda
functions, compared to the three lines typically needed with a traditional function definition using def
.
In the previous example, while the lambda function add_lam
is assigned to a variable, it's more commonly utilized directly as an argument within functions like map()
, filter()
or sorted()
.
Use Lambda Function with the map()
Function
We want to double only odd values in the numbers list.
# Create a listnumbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x*2 if x%2 != 0 else x, numbers))
print(squared)
Output:
[2, 2, 6, 4, 10]
Explanation
lambda x: x*2 if x % 2 != 0 else x
: This lambda function takes an input x as an argument and returnsx*2
if x is odd (i.e.,x % 2 != 0
), otherwise it returns x unchanged.map()
applies this lambda function to each element in thenumbers
list.The
list()
function is used to convert the result ofmap()
into a list.
Thus, for odd numbers in the numbers
list (1, 3, and 5), the lambda function doubles them (x*2)
because x % 2 != 0
condition is met.
For even numbers in the numbers
list (2 and 4), the lambda function returns them unchanged (x) because the else
part is triggered.
Use Lambda Function with the filter()
Function
# Create a listnumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)
Output:
[2, 4, 6, 8, 10]
Explanation
lambda x: x % 2 == 0
: This lambda function takes an input x and returns True if x is even (i.e.,x % 2 == 0
), and False otherwise.The
filter()
function applies the lambda function on each element in thenumbers
list and retains only those elements for which the lambda function returns True.The
list()
function is used to convert the filtered result into a list.
Thus the lambda function filters out only the even numbers from the numbers
list, resulting in a new list containing only those even numbers.
If we anticipate reusing a function multiple times, it's typically more appropriate to define it using def
rather than using a lambda function. Lambda functions are often employed to simplify code, particularly when the function is intended for a one-time use.