Lambda Functions
A lambda function is a small, anonymous function that Python lets you define in a single line, without giving it a name using def. Lambdas are handy for short, throwaway pieces of logic, especially when you need to pass a tiny function somewhere else as an argument.
What is a Lambda Function?
A lambda is created with the lambda keyword, followed by a comma-separated list of parameters, a colon, and a single expression. The result of that expression is automatically returned — there is no return statement.
# a lambda that doubles a number
double = lambda x: x * 2
print(double(5)) # 10
# a lambda with two parameters
add = lambda a, b: a + b
print(add(3, 4)) # 7
This is exactly equivalent to writing a regular function:
def double(x):
return x * 2
Lambda vs. a Regular def Function
Lambdas are not meant to replace regular functions everywhere — they exist for a specific niche. Use a lambda when the logic is short and disposable, and you mainly need it in the same line where it is used, often as an argument to another function. Use a regular def function when the logic has a name worth documenting, spans more than one expression, needs a docstring, or will be reused in multiple places.
Note: If you find yourself struggling to squeeze logic into a single expression, that is usually a sign you should write a proper def function instead of forcing it into a lambda.
Lambda with sorted()
One of the most common uses of a lambda is as the key argument for sorted(), telling Python exactly what value to sort by.
people = [
{"name": "Amit", "age": 32},
{"name": "Priya", "age": 25},
{"name": "Rahul", "age": 40},
]
# sort the list of dictionaries by age
by_age = sorted(people, key=lambda p: p["age"])
for person in by_age:
print(person["name"], person["age"])
# Priya 25
# Amit 32
# Rahul 40
Without a lambda you would have to write and name a separate helper function just to extract the sort key, which is far more code for a one-off comparison.
Lambda with map()
map() applies a function to every item in an iterable and returns an iterator of the results. A lambda is a natural fit when the transformation is simple.
numbers = [1, 2, 3, 4, 5]
# square every number in the list
squares = list(map(lambda n: n ** 2, numbers))
print(squares) # [1, 4, 9, 16, 25]
Lambda with filter()
filter() keeps only the items from an iterable for which the given function returns a truthy value. Again, a lambda expresses the condition without any extra ceremony.
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
# keep only the even numbers
evens = list(filter(lambda n: n % 2 == 0, numbers))
print(evens) # [2, 4, 6, 8]
The Main Limitation of Lambdas
A lambda body can only be a single expression — it cannot contain statements such as assignments, if/else blocks spanning multiple lines, loops, or multiple lines of logic. This is a deliberate design choice that keeps lambdas short and readable, but it also means anything more involved than a one-liner needs a regular def function instead.
# OK: a single conditional expression is allowed
label = lambda n: "even" if n % 2 == 0 else "odd"
print(label(7)) # odd
# NOT allowed: multiple statements inside a lambda
# bad = lambda x: total = x + 1; return total -> SyntaxError