A lambda function in Python is a concise, anonymous function defined using the lambda keyword.
These functions are often used for simple operations, especially when passing them as arguments to higher-order functions like map(), filter(), and reduce().
Syntax:
lambda arguments: expression- Arguments: Can be zero, one, or multiple.
- Expression: A single expression whose result is returned when the lambda function is called.
Key Features
- Anonymous: Unlike regular functions, lambda functions are not bound to a name.
- Single Expression: Lambda functions can only contain a single expression, which is automatically returned.
- Short-lived: Primarily used in places where small, simple functions are needed temporarily.
Examples
Basic Lambda Function:
Equivalent to a function that doubles a number:
# Regular function
def double(x):
return x * 2
# Lambda function
double_lambda = lambda x: x * 2
print(double_lambda(5)) # Output: 10Lambda Function with Multiple Arguments:
Calculate the product of two numbers:
# Regular function
def multiply(x, y):
return x * y
# Lambda function
multiply_lambda = lambda x, y: x * y
print(multiply_lambda(3, 4)) # Output: 12Lambda with No Arguments:
Return a fixed value:
constant_lambda = lambda: 42
print(constant_lambda()) # Output: 42Using Lambda with Higher-Order Functions
-
map(): Applies a function to each element of an iterable.numbers = [1, 2, 3, 4] squared = map(lambda x: x**2, numbers) print(list(squared)) # Output: [1, 4, 9, 16] -
filter(): Filters elements of an iterable based on a condition.numbers = [1, 2, 3, 4, 5, 6] even_numbers = filter(lambda x: x % 2 == 0, numbers) print(list(even_numbers)) # Output: [2, 4, 6] -
reduce(): Reduces an iterable to a single value (requiresfunctools).from functools import reduce numbers = [1, 2, 3, 4] product = reduce(lambda x, y: x * y, numbers) print(product) # Output: 24
Lambda vs Regular Functions
| Aspect | Lambda Function | Regular Function |
|---|---|---|
| Definition | Defined using lambda. |
Defined using def. |
| Name | Anonymous (no name). | Always has a name. |
| Complexity | Limited to a single expression. | Can include multiple statements and logic. |
| Use Case | Short-lived, simple operations. | General-purpose, reusable logic. |
| Readability | Less readable for complex logic. | More readable and maintainable. |
When to Use Lambda Functions
- Inline functions: When you need a small function for a single use (e.g., sorting, filtering).
- As arguments: Passing functions to higher-order functions like
map(),filter(), orreduce(). - Short, simple logic: When the function body is only one expression.
When to Avoid Lambdas
Avoid lambdas when:
- The logic is complex or spans multiple steps.
- Readability is more important (e.g., in production code).
Best Practices
- Keep lambdas short and simple, if it doesn’t fit on one line, use
def. - Use them where they improve clarity, not just for brevity.
- Combine with built-ins (map, filter, reduce) for clean, functional-style code.
- Don’t overuse, regular functions are better for reusability and debugging.
👉 Next tutorial: Map, Filter and Reduce in Python