In Python, a lambda function is a small anonymous function defined with the lambda keyword. Unlike regular functions defined using def, lambda functions are typically used for short, throwaway functions that are not meant to be reused elsewhere. They are often used when you need a simple function for a short period and don’t want to formally define it.
Basic Syntax
The syntax for a lambda function is:
lambda arguments: expression
- lambda: The keyword used to define the lambda function.
- arguments: The parameters the function takes (similar to parameters in regular functions).
- expression: A single expression that the lambda function evaluates and returns. Unlike regular functions, lambda functions cannot contain multiple expressions or statements.
Examples of Lambda Functions
Basic Lambda Function
# Define a lambda function that adds 10 to a given number
add_ten = lambda x: x + 10
print(add_ten(5)) # Output: 15
Here, lambda x: x + 10 is a lambda function that takes one argument x and returns x + 10. It’s equivalent to the following regular function:
def add_ten(x):
return x + 10
Lambda Function with Multiple Arguments
# Define a lambda function that multiplies two numbers
multiply = lambda x, y: x * y
print(multiply(4, 5)) # Output: 20
In this case, the lambda function lambda x, y: x * y takes two arguments and returns their product.
Lambda Function as an Argument
Lambda functions are often used as arguments to higher-order functions like map(), filter(), and sorted().
Using map()
numbers = [1, 2, 3, 4, 5]
squared_numbers = map(lambda x: x ** 2, numbers)
print(list(squared_numbers)) # Output: [1, 4, 9, 16, 25]
Here, the lambda function lambda x: x ** 2 squares each number in the numbers list.
Using filter()
numbers = [1, 2, 3, 4, 5]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
print(list(even_numbers)) # Output: [2, 4]
In this example, lambda x: x % 2 == 0 filters out only the even numbers from the numbers list.
Using sorted()
# Sort a list of tuples based on the second element
data = [(1, 'apple'), (2, 'banana'), (3, 'cherry')]
sorted_data = sorted(data, key=lambda x: x[1])
print(sorted_data) # Output: [(1, 'apple'), (2, 'banana'), (3, 'cherry')]
The lambda function lambda x: x[1] sorts the list of tuples based on the second element of each tuple.
Lambda Function in a List Comprehension
# Create a list of squared numbers using a lambda function
numbers = [1, 2, 3, 4, 5]
squared_numbers = [(lambda x: x ** 2)(x) for x in numbers]
print(squared_numbers) # Output: [1, 4, 9, 16, 25]
Here, the lambda function is used within a list comprehension to generate a list of squared numbers.
Characteristics of Lambda Functions
- Anonymous: Lambda functions do not have a name unless explicitly assigned to a variable.
- Single Expression: They are limited to a single expression and cannot contain statements or multiple expressions.
- Compact: They are generally used for small, quick tasks where a full function definition might be overkill.
- Functional Programming: Lambda functions are often used in conjunction with functional programming techniques like map, filter, and reduce.
Lambda functions are a powerful tool in Python for scenarios where a simple, short function is needed temporarily. They provide a concise way to create functions without the need for formal function definitions.