A lambda function is a small anonymous function in Python. "Anonymous" means a function that is without a name. While normal functions are defined using the def keyword, in Python anonymous functions are defined using the lambda keyword.
The syntax of a lambda function is quite simple:
lambda arguments: expressionlambda: A keyword that signifies that this is a lambda function.arguments: (Optional) The parameters or inputs to the function.expression: A single expression that the function returns.
- Anonymity: Lambda functions are anonymous, meaning they are not declared with the standard
defkeyword. - Simplicity: They can have any number of parameters but can only have one expression.
- Usage: They are used for short-duration, non-reusable operations.
- Return: The expression value is returned on execution.
Here's a basic example to illustrate a lambda function:
# Lambda function that adds 10 to the number passed in as an argument
f = lambda x: x + 10
print(f(5)) # Output: 15-
Single-use or temporary functions: When you need a function for a short period of time and do not want to formally define it.
-
Functional arguments: Passing a function as an argument to higher-order functions, such as
map(),filter(), andreduce().
map() applies a given function to all the items in an input list (or any other iterable).
my_list = [1, 2, 3, 4, 5]
squared_list = list(map(lambda x: x**2, my_list))filter() creates a list of elements for which a function returns True.
my_list = [1, 2, 3, 4, 5]
even_list = list(filter(lambda x: x % 2 == 0, my_list))reduce() applies a rolling computation to sequential pairs of values in a list.
from functools import reduce
my_list = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, my_list)Let's check the output of the extended examples by running the code snippets.
from functools import reduce
# Example 1: Using lambda with map()
my_list = [1, 2, 3, 4, 5]
squared_list = list(map(lambda x: x**2, my_list))
# Example 2: Using lambda with filter()
even_list = list(filter(lambda x: x % 2 == 0, my_list))
# Example 3: Using lambda with reduce()
product = reduce(lambda x, y: x * y, my_list)
(squared_list, even_list, product)
# RESULT
([1, 4, 9, 16, 25], [2, 4], 120)my_list = [1, 2, 3, 4, 5]
squared_list = list(map(lambda x: x**2, my_list))
# Output: 1,4,9,16,251,4,9,16,25Each element of my_list is squared.
my_list = [1, 2, 3, 4, 5]
even_list = list(filter(lambda x: x % 2 == 0, my_list))
# Output: 2,42,4Only the even numbers are retained from my_list.
my_list = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, my_list)
# Output: 120The product of all elements in my_list is calculated as (1×2×3×4×5=120)(1×2×3×4×5=120).
Lambda functions can be very useful for performing quick calculations without defining a full function. However, they should be used judiciously to ensure that code remains readable. If a computation is complex or requires documentation, it's often better to define a traditional function using def.