Last modified: Feb 02, 2026 By Alexander Williams

Python Lambda Functions Guide & Examples

Python offers powerful tools for writing clean code. One such tool is the lambda function.

It is a small, anonymous function defined with the lambda keyword. It is perfect for short, simple operations.

This guide explains everything you need to know. We will cover syntax, use cases, and best practices.

What is a Lambda Function?

A lambda function is an anonymous, inline function. It is defined without a name using the lambda keyword.

It can take any number of arguments but must have only one expression. The result of this expression is automatically returned.

This makes lambdas ideal for simple tasks. They are often used where a small function is needed temporarily.

For a deeper dive into standard function creation, see our Python Function Syntax Guide for Beginners.

Lambda Function Syntax

The syntax is straightforward. It follows this pattern:


# lambda arguments: expression
square = lambda x: x ** 2
print(square(5))
    

25
    

Here, lambda x: x ** 2 creates a function. It takes one argument x and returns x ** 2.

The function is assigned to the variable square. We then call it like a regular function.

The key difference from def is the lack of a name and a return statement. The expression's result is implicitly returned.

Common Use Cases for Lambda

Lambda functions shine when used with built-in functions. These include map(), filter(), and sorted().

1. With map() Function

The map() function applies a given function to each item in an iterable. A lambda is perfect here.


numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x * x, numbers))
print(squared_numbers)
    

[1, 4, 9, 16, 25]
    

The lambda lambda x: x * x squares each number. map() applies it to the entire list.

2. With filter() Function

The filter() function constructs an iterator from elements of an iterable for which a function returns true.


numbers = [10, 15, 21, 30, 45]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)
    

[10, 30]
    

The lambda lambda x: x % 2 == 0 checks if a number is even. filter() keeps only those that are True.

3. With sorted() Function

The sorted() function can use a lambda as a key argument for custom sorting.


students = [('Alice', 22), ('Bob', 19), ('Charlie', 25)]
# Sort by age (the second element in each tuple)
sorted_by_age = sorted(students, key=lambda student: student[1])
print(sorted_by_age)
    

[('Bob', 19), ('Alice', 22), ('Charlie', 25)]
    

The lambda lambda student: student[1] tells sorted() to use the age (index 1) for comparison.

For more on handling function arguments effectively, explore our guide on Python Function Argument Unpacking.

Lambda vs. Regular Functions (def)

Knowing when to use lambda versus def is important.

Use a lambda function when:

  • The function logic is a simple, one-line expression.
  • The function is needed temporarily, often as an argument to another function.
  • You want to avoid cluttering your code with many small, named functions.

Use a regular def function when:

  • The function body requires multiple lines or complex logic.
  • The function needs a descriptive name for documentation and readability.
  • You need to use statements like if-elif-else, loops, or try-except.
  • The function will be reused multiple times in your code.

Limitations and Best Practices

Lambda functions are powerful but have limits.

They can only contain a single expression. You cannot use statements like print, if, or for loops directly inside the expression.

For complex conditions, use a conditional expression (ternary operator).


# Using a conditional expression inside a lambda
categorize = lambda x: 'high' if x > 50 else 'low'
print(categorize(60))
print(categorize(30))
    

high
low
    

Best Practice: If your lambda becomes hard to read, switch to a def function. Readability is a core Python principle.

Do not assign a lambda to a variable if you plan to use it like a regular function. Just use def for clarity.

Conclusion

Python lambda functions are a valuable tool. They provide a concise way to write simple, anonymous functions.

They are most effective when used with functions like map, filter, and sorted.

Remember their key trait: they evaluate and return a single expression.

Use lambdas for simplicity and inline operations. Use regular def functions for complexity and reusability.

Mastering lambda functions will help you write more functional and elegant Python code. Start by integrating them into your next script with map() or filter().