logo

Lambda Functions

Lambda functions are small, unnamed functions you define inline. They're perfect for simple operations you only need once.

# Regular function
def double(x):
    return x * 2

# Lambda equivalent
double = lambda x: x * 2

Lambdas shine when passed to other functions:

numbers = [3, 1, 4, 1, 5]
sorted(numbers, key=lambda x: -x)  # [5, 4, 3, 1, 1]

The syntax is: lambda arguments: expression. No def, no return, no name. Just the transformation itself.

Lambdas have limits:

  • Only one expression (no statements)
  • No assignments inside
  • Hard to debug (no name in tracebacks)

Use lambdas for simple, obvious operations. If you need multiple lines or complex logic, write a regular function.

I explain when to use lambdas in my Functional Programming course.