Easy👤 0-2 years👤 3-5 years 1 min read

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

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

Syntaxlambda x: x * 2
BodyONE expression, no statements
Best usekey= in sorted/min/max, quick callbacks
Avoidcomplex 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 functionno statementskey= use caseprefer def for complex logicPEP 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.
Open inChatGPTGeminiClaude

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