lambda Anonymous function
Last updated: 2026-09-22
lambda Anonymous function
lambda creates an anonymous function (single expression).
| Category | Keywords & Statements |
|---|---|
| Kind | Keyword / Statement |
| Python Version | all |
📝 Syntax
lambda args: expression
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
10
[3, 2, 1]
🏷️ Related Keywords / Statements
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QCan a `lambda` contain multiple lines of code?
ANo. A `lambda` body can only be a single expression. An `if`/`else` can be replaced by a conditional expression (`x if cond else y`), but multi-line logic should use `def`.
QWhat is `lambda` useful for?
AMainly where you need a short function as an argument, e.g. `sorted(data, key=lambda x: x[1])`, `map`, or `filter`. Do not use `lambda` for complex logic — readability suffers.
QWhat is the difference between functions defined by `lambda` and by `def`?
ABoth are function objects with equivalent behavior. The difference is that `lambda` has no name, cannot have a docstring, and holds only a single expression; `def` is more general and the default choice.