Lambda Functions

Learn Python lambda functions with examples. Understand syntax, features, and how to use lambda with map, filter, and reduce for concise code.

Loading...
Lambda Functions

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

  1. Anonymous: Unlike regular functions, lambda functions are not bound to a name.
  2. Single Expression: Lambda functions can only contain a single expression, which is automatically returned.
  3. 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: 10

Lambda 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: 12

Lambda with No Arguments:

Return a fixed value:

constant_lambda = lambda: 42
print(constant_lambda())  # Output: 42

Using Lambda with Higher-Order Functions

  1. 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]
  2. 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]
  3. reduce(): Reduces an iterable to a single value (requires functools).

    from functools import reduce
     
    numbers = [1, 2, 3, 4]
    product = reduce(lambda x, y: x * y, numbers)
    print(product)  # Output: 24

Learn more about map, filter and reduce in Python!


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(), or reduce().
  • 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

Support my work!