EasyπŸ‘€ 0-2 yearsπŸ‘€ 3-5 years 1 min read

What is a lambda function and when should you use one?

Reviewed by Gurusankar M.

Asked inInfosysAccentureDeloitteCognizant
#lambda#anonymous function#sorted#key#functional
Report issue

⚑ 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`.

β˜•Coffee Chat Question

Concept Made Simple

β€œWhat is a lambda function and when should you use one?”

🧠Mind Map Answer

Remember It Faster

Syntax→lambda x: x * 2
Body→ONE expression, no statements
Best use→key= in sorted/min/max, quick callbacks
Avoid→complex logic → use def

⌨️Hands-on Keyboard

Learn by Doing

python
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))
Output
[('Asha', 25), ('Ravi', 28), ('Guru', 30)]
10

πŸ”₯What If?

Think Beyond the Expected

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

  • 1How does lambda pair with map/filter/reduce?
  • 2Why does PEP 8 discourage naming a lambda?
  • 3What is a closure and does lambda capture variables?

🧩Related Technologies

map/filtersorted keyclosuresfunctools

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?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission β€” at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions