What are Lambda Functions in Python?
Learn what Python lambda functions are, the lambda syntax, and how to use them with sorted, map and filter, plus code examples and common interview questions.
Expected Interview Answer
A lambda function is a small anonymous function defined inline with the lambda keyword, consisting of a single expression whose result is returned automatically without a def statement or a name.
The syntax is lambda arguments: expression. Lambdas can take any number of arguments but hold exactly one expression — no statements, loops, or multiple lines. They shine as short throwaway functions passed to higher-order functions like sorted(), map(), and filter(), where writing a full named function would be overkill.
- Concise one-line function definitions
- No need to name a throwaway function
- Ideal as arguments to sorted, map and filter
- Keeps small logic close to where it is used
- Reduces boilerplate for simple transformations
AI Mentor Explanation
A lambda is like a quick single taken on instinct rather than a planned boundary shot with a full backlift. It exists only for that one delivery, does its small job of rotating the strike, and needs no elaborate setup or name in the coaching manual.
Step-by-Step Explanation
Step 1
Start with lambda
Write the lambda keyword to declare an anonymous function inline.
Step 2
List the arguments
Add zero or more parameters, comma-separated, before the colon.
Step 3
Write one expression
After the colon, give a single expression whose value is returned automatically.
Step 4
Use it inline
Pass it directly to functions like sorted(), map(), or filter() as the key or callback.
Step 5
Prefer def when complex
If logic needs statements or multiple lines, switch to a named def function.
What Interviewer Expects
- Correct lambda syntax: lambda args: expression
- Knowing a lambda holds one expression, not statements
- Common use with sorted, map and filter
- Understanding it is anonymous and often throwaway
- Knowing when a named def is the better choice
Common Mistakes
- Trying to put statements or multiple lines inside a lambda
- Adding an explicit return inside a lambda
- Overusing lambdas where a named function reads better
- Assigning a lambda to a variable when def is clearer
- Expecting loops or assignments to work in a lambda body
Best Answer (HR Friendly)
“A lambda is a small, unnamed function you can write in a single line using the lambda keyword. It is handy for quick, simple tasks like sorting or transforming data, where creating a full named function would be more than you need.”
Code Example
square = lambda x: x * x
print(square(5)) # 25
people = [('Ada', 36), ('Sam', 28), ('Lee', 41)]
# Sort by age using a lambda key
people.sort(key=lambda person: person[1])
print(people) # [('Sam', 28), ('Ada', 36), ('Lee', 41)]nums = [1, 2, 3, 4, 5, 6]
doubled = list(map(lambda n: n * 2, nums))
print(doubled) # [2, 4, 6, 8, 10, 12]
evens = list(filter(lambda n: n % 2 == 0, nums))
print(evens) # [2, 4, 6]Follow-up Questions
- What is the difference between a lambda and a def function?
- Why can't a lambda contain statements?
- How does a lambda work as a key argument in sorted()?
- When should you avoid using a lambda?
- Can a lambda capture variables from its enclosing scope?
MCQ Practice
1. How many expressions can a lambda body contain?
A lambda holds exactly one expression whose value is returned automatically; it cannot contain statements.
2. Which is a valid lambda that adds two numbers?
The syntax is lambda arguments: expression, with no explicit return keyword.
3. Where are lambdas most commonly used?
Lambdas are ideal as short inline callbacks, such as the key argument to sorted() or the function passed to map() and filter().
Flash Cards
What is the lambda syntax? — lambda arguments: expression — the expression's value is returned automatically.
How many expressions can a lambda hold? — Exactly one; it cannot contain statements or multiple lines.
Is a lambda named? — No — it is anonymous, though it can be assigned to a variable.
A common use for lambdas? — As the key or callback in sorted(), map(), and filter().