What is a lambda function and when should you use one?
Reviewed by Gurusankar M.
Short Answer
A lambda is a small anonymous function written inline: `lambda args: expression`. It holds a single expression (no statements) and is handy as a short throwaway callback — e.g. the `key=` for sorted/min/max. For anything non-trivial, use a named `def`.
On this page
Concept Made Simple
Coffee Chat Question
“What is a lambda function and when should you use one?”
Remember It Faster
Mind Map Answer
- Syntax
- lambda x: x * 2
- Body
- ONE expression, no statements
- Best use
- key= in sorted/min/max, quick callbacks
- Avoid
- complex logic → use def
Learn by Doing
Hands-on Keyboard
people = [("Guru", 30), ("Asha", 25), ("Ravi", 28)]
by_age = sorted(people, key=lambda p: p[1])
print(by_age)
double = lambda x: x * 2
print(double(5))[('Asha', 25), ('Ravi', 28), ('Guru', 30)]
10Think Beyond the Expected
What If?
When is a named `def` better than a lambda?
When the logic spans more than one expression, needs statements (loops, try/except), or is reused — a def is clearer, testable, and shows a name in tracebacks. Assigning a lambda to a variable (`f = lambda ...`) is discouraged by PEP 8; just use def.
Real World
Lambdas shine as the `key` for sorting or as tiny callbacks in map/filter/GUI handlers; beyond a one-liner, teams prefer named functions for readability and debugging.
Interviewer's Expectation
Keywords they're listening for:
- anonymous single-expression function
- no statements
- key= use case
- prefer def for complex logic
- PEP 8 note
Common Mistakes
- Cramming complex logic into a lambda
- Assigning lambdas to names instead of using def
- Late-binding closure surprises in loops
Best Practices
- Use lambda for short inline callbacks
- Use def for anything reusable or multi-line
- Common home: key= in sorted/min/max
Follow-up Questions
- How does lambda pair with map/filter/reduce?
- Why does PEP 8 discourage naming a lambda?
- What is a closure and does lambda capture variables?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Lambda (Python) Interview question: "What is a lambda function and when should you use one?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair