Compact anonymous functions for expressions that don't need a name
Lambda shows up everywhere Python uses functions as values — sorting, filtering, mapping, and callbacks. Knowing the syntax, knowing where it helps, and knowing when a def is cleaner signals idiomatic Python fluency.
Lambda makes functions first-class — you can pass them, return them, and compose them inline without naming them. Understanding this unlocks map, filter, sorted, and the full functional toolkit.
A lambda is a function with no name. lambda x: x * 2 is exactly the same as writing def f(x): return x * 2 — except you can't write multiple statements or use return. The expression after the colon is the return value.
Think of it as a throwaway function — useful when you need a function once, inline, and naming it would just add noise.
A lambda is an anonymous function defined in a single expression. Arguments go before the colon, the expression after it is the return value — no return keyword needed.
A lambda takes any number of arguments, separated by commas, and returns the result of a single expression. No return keyword — the expression is the return value.
Lambdas appear most often as short-lived key functions passed to sorted, map, and filter — where naming the function would add noise without adding clarity.
sorted with a key lambda is the most common use in practice. map and filter are often replaced by list comprehensions, but lambda makes them readable for simple cases.
Lambdas are limited to a single expression — no statements, assignments, or branching logic. Anything with multiple steps or reuse is cleaner as a named function.
Use a lambda when the function is short, used once, and naming it adds no clarity. Use def when there is branching logic, multiple steps, or the function will be reused — named functions are easier to test and debug.