A Function You Don't Bother Naming
Most functions in Python are defined with def, have names, and live in a file where they can be reused. A lambda is a smaller, unnamed alternative — useful in the handful of cases where defining a full function would be more ceremony than it's worth.
Here's the shape side by side:
Two functions, both doing the exact same thing. The lambda version fits on one line because that's basically its job: a throwaway function for a throwaway moment.
Syntax
lambda <params>: <expression>
<params>is the same parameter list adefwould use — supports defaults,*args,**kwargs.<expression>is a single expression whose value becomes the return value.
Examples:
No return keyword. The expression's value is returned implicitly.
The One Limitation
A lambda can contain only a single expression. You can't put statements in it:
But you can use the ternary expression:
If you need more than one expression, that's your cue to switch to def.
Where Lambdas Actually Earn Their Keep
A lambda assigned to a variable is almost always worse than a named def:
That's strictly worse than def double(x): return x * 2 — same readability, plus the lambda version gives debuggers the unhelpful name <lambda> in stack traces.
Lambdas shine in exactly one spot: as inline arguments to higher-order functions.
Custom sort keys
The classic case:
The key argument of sorted wants a function that takes an item and returns the value to sort by. A one-line lambda is exactly right — and using a full def would feel heavy for something this small.
You can sort by multiple fields using a tuple:
min, max, filter, map
Similar story — they all accept an optional function argument:
For map and filter, a list comprehension is usually even clearer:
Most Pythonistas reach for comprehensions over map/filter for this reason. lambda remains a natural fit for sorted, min, and max where there's no comprehension shortcut.
Lambdas and operator
When your lambda is just "grab a key" or "grab an attribute," Python's operator module has faster, clearer alternatives:
Not a huge win, but in code that already imports operator it reads a bit more declaratively.
When to Switch Back to def
If any of these are true, write a def:
- The logic doesn't fit cleanly in a single expression.
- You want to name the function because the name would aid readability.
- You'd use the same logic in more than one place.
- The function needs a docstring (lambdas can't carry one).
Remember: Python isn't scored on line count. A three-line named function almost always reads better than a one-line lambda once the logic is non-trivial.
Next: Decorators
Lambdas hand functions around as values. Decorators take the same idea one step further: they let you wrap any function with reusable behavior — logging, timing, caching, auth — using a single @ on the line above. That's the next page.
Frequently Asked Questions
What is a lambda in Python?
A lambda is a short, anonymous function you can write on a single line. lambda x: x * 2 is equivalent to def double(x): return x * 2, except without a name. It's most useful when you need a tiny function as an argument to another function.
When should I use lambda instead of def?
Use lambda for one-off, one-expression functions that you pass directly to something like sorted, max, or filter. For anything with a body longer than a single expression — or for anything you'd name — use def. Named functions are almost always clearer.
What are the limits of lambda?
A lambda can contain only a single expression, not statements. No assignments, no if blocks (though you can use the ternary a if cond else b), no multi-line logic. If you're fighting those limits, switch to def.