Python theorytheory 0/50 · 0%
Functions · easy

14. Lambda expressions

Anonymous single-expression functions.

`lambda` creates a small, anonymous function limited to a single expression — no statements, no multiple lines.

python
double = lambda x: x * 2
pairs = [("eth", 3), ("btc", 1)]
pairs.sort(key=lambda p: p[1])

Lambdas are most useful as short throwaway callbacks passed to `sorted`, `map`, or `filter`; for anything nontrivial, a named `def` function is clearer and easier to test.

Check your understanding

  1. 1. What can a lambda body contain?

  2. 2. Where are lambdas most commonly used?

  3. 3. What does `lambda x: x * 2` do when called with 5?