Last modified: Nov 19, 2026
Python Lambda Function: Syntax and Examples
A lambda function is a small anonymous function in Python. It has no name. It can take many arguments but holds only one expression. Many beginners meet lambda and feel confused. This guide explains the syntax step by step. It also gives real examples you can run right away.
What Is a Lambda Function?
A lambda function is a shortcut for a tiny function. You write it in one line. It returns the value of its expression automatically. You do not need a return keyword.
Think of it as a throwaway function. You use it once, often inside another function. It keeps your code short and clean.
Regular functions use the def keyword. Lambda functions use the lambda keyword. Both create callable objects. The difference is size and style.
Lambda Function Syntax
The syntax is simple. It follows this pattern:
lambda arguments: expression
There are three parts. The lambda keyword starts it. The arguments list comes next. A colon separates them from the expression. The expression is the result.
Here is a basic example. It adds two numbers:
# A lambda that adds two numbers
add = lambda x, y: x + y
print(add(3, 5))
8
You can also call a lambda directly without naming it:
# Call the lambda immediately
print((lambda x: x * 2)(10))
20
Lambda vs Regular Function
Compare both styles. First, a normal function:
# Regular function
def square(n):
return n * n
print(square(4))
16
Now the same logic as a lambda:
# Lambda version
square = lambda n: n * n
print(square(4))
16
Both give the same result. The lambda version is shorter. But it is harder to read when logic grows. Use lambdas for simple tasks only.
Lambda With No Arguments
A lambda can take zero arguments. Just leave the argument list empty:
# Lambda with no arguments
greet = lambda: "Hello, Python!"
print(greet())
Hello, Python!
Lambda With Default Arguments
Lambdas support default values. This works like regular functions:
# Default argument in a lambda
power = lambda base, exp=2: base ** exp
print(power(5))
print(power(5, 3))
25
125
Lambda Inside map()
The map() function applies a function to every item. A lambda fits well here. This example doubles each number:
# Double each number with map and lambda
numbers = [1, 2, 3, 4]
doubled = list(map(lambda x: x * 2, numbers))
print(doubled)
[2, 4, 6, 8]
You can pair this with other iterable tools. For example, the Python zip function combines iterables into pairs, and a lambda can process each pair.
# Multiply paired values
nums = [1, 2, 3]
multipliers = [10, 20, 30]
result = list(map(lambda pair: pair[0] * pair[1], zip(nums, multipliers)))
print(result)
[10, 40, 90]
Lambda Inside filter()
The filter() function keeps items that pass a test. Use a lambda to define the test:
# Keep even numbers only
numbers = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens)
[2, 4, 6]
This pattern is handy when you loop with an index. If you also need positions, the Python enumerate complete guide shows how to get both index and value in one loop.
Lambda With sorted()
The sorted() function accepts a key function. A lambda makes a great key. This example sorts words by length:
# Sort words by their length
words = ["apple", "fig", "banana", "kiwi"]
sorted_words = sorted(words, key=lambda w: len(w))
print(sorted_words)
['fig', 'kiwi', 'apple', 'banana']
You can also sort a list of tuples by one field:
# Sort students by score
students = [("Ana", 88), ("Ben", 95), ("Cara", 79)]
ranked = sorted(students, key=lambda s: s[1], reverse=True)
print(ranked)
[('Ben', 95), ('Ana', 88), ('Cara', 79)]
This is similar to finding extremes. The Python find max and min in a list article shows how to use a key with max and min too.
Lambda With reduce()
The reduce() function folds a list into one value. It lives in the functools module. A lambda works as the combining step:
# Multiply all numbers together
from functools import reduce
numbers = [1, 2, 3, 4]
product = reduce(lambda a, b: a * b, numbers)
print(product)
24
Lambda in a Dictionary
You can store lambdas in a dictionary. This creates a simple dispatch table:
# Map operators to lambdas
operations = {
"add": lambda a, b: a + b,
"sub": lambda a, b: a - b,
"mul": lambda a, b: a * b,
}
print(operations["add"](6, 2))
print(operations["mul"](6, 2))
8
12
This idea powers many small tools. For instance, a Python simple calculator program can use a dictionary of lambdas for each operator.
Conditional Expressions in Lambda
A lambda can hold a conditional expression. It cannot hold a full if statement. Use the inline form instead:
# Return "even" or "odd"
check = lambda n: "even" if n % 2 == 0 else "odd"
print(check(7))
print(check(10))
odd
even
This works well for quick decisions. You can apply it to real problems, like the Python leap year program with examples or a Python convert Celsius to Fahrenheit task.
Common Mistakes
Beginners often hit the same problems. Keep these rules in mind.
One expression only. A lambda cannot contain statements. No return, no print, no loops. Only a single expression.
Readability first. Long lambdas are hard to read. If logic needs more than one line, use def instead.
Late binding in loops. Lambdas inside loops capture the variable, not its value. Check this example:
# Late binding problem
funcs = [lambda: i for i in range(3)]
print([f() for f in funcs])
[2, 2, 2]
The fix is a default argument. It locks the value at creation time:
# Fixed with a default argument
funcs = [lambda i=i: i for i in range(3)]
print([f() for f in funcs])
[0, 1, 2]
When to Use a Lambda
Use a lambda for short, one-time logic. Good cases include keys for sorting, filters, and map steps. Avoid lambdas when the logic is complex. Avoid them when you need debugging or a docstring. Named functions are better there.
Lambda also pairs well with text processing. For example, you can use it with regex helpers. If you compare the Python match vs search in regex methods, you will see how functions and lambdas fit into pattern work.
Conclusion
Lambda functions are a compact tool in Python. The syntax is short: lambda arguments: expression. They shine inside map, filter, sorted, and reduce. They keep small operations tidy.
Still, keep your code readable. Use lambdas for simple jobs. Switch to regular functions when logic grows. Practice with the examples above, and lambda will become a natural part of your Python toolkit.