What are Lambda Functions in Python?
Learn what Python lambda functions are, their syntax, common uses with sorted/map/filter, limitations, and when to use a named function instead.
Expected Interview Answer
A lambda function is a small, anonymous, single-expression function defined inline with the lambda keyword — lambda arguments: expression — used when you need a short throwaway function, most often passed directly as an argument to something like sorted(), map(), or filter().
Unlike a regular function defined with def, a lambda has no name (though you can assign it to a variable), no statements or multiple lines, and no explicit return — the single expression's value is returned automatically. Lambdas can take any number of arguments, including defaults and *args/**kwargs, just like a normal function, but the body is restricted to one expression, so you cannot use assignments, loops, or multiple statements inside one. They are most idiomatic as a key function for sorted(), min(), max(), or as the function argument to map()/filter(), where naming a separate function would add unnecessary ceremony for one-off logic. For anything beyond a trivial expression, PEP 8 and most style guides recommend a regular named def function instead, both for readability and because named functions get proper tracebacks and docstrings.
- Concise inline syntax for small, throwaway logic
- Ideal as a key= argument for sorted(), min(), max()
- Avoids cluttering a module with tiny one-off named functions
- Supports default arguments and *args/**kwargs like normal functions
- Can be assigned to a variable for reuse when appropriate
AI Mentor Explanation
A lambda function is like a substitute fielder called on for one specific play — no formal contract, no name announced on the scoreboard, just a quick single action performed and then they're off. You'd use one to briefly cover a single catch, but for a full match role you'd sign a proper named player instead, the way a def function handles anything beyond one throwaway task.
Step-by-Step Explanation
Step 1
Syntax
lambda arguments: expression defines an anonymous function; the expression's value is returned implicitly.
Step 2
No statements allowed
The body must be a single expression — no assignments, loops, or multiple statements.
Step 3
Arguments work like normal
Lambdas support positional args, default values, and *args/**kwargs just like def functions.
Step 4
Typical use: key functions
sorted(data, key=lambda x: x.age) is the most idiomatic use case.
Step 5
Typical use: map/filter
map(lambda x: x * 2, nums) or filter(lambda x: x > 0, nums) apply the lambda per item.
Step 6
When to avoid
For anything beyond a trivial expression, prefer a named def function for readability and debuggability.
What Interviewer Expects
- Correctly writes lambda syntax and knows it returns implicitly
- Knows lambdas are limited to a single expression, no statements
- Gives idiomatic use cases (sorted key=, map, filter)
- Knows when NOT to use a lambda (complex logic should be a def function)
- Understands lambdas can capture variables from enclosing scope (closures)
Common Mistakes
- Trying to put multiple statements or an if/else block that isn't a conditional expression inside a lambda
- Overusing lambdas for complex logic, hurting readability and debuggability
- Forgetting a lambda has no name for tracebacks, making debugging harder
- Confusing lambda with a regular function decorator or generator
Best Answer (HR Friendly)
“A lambda function is a short, unnamed function you define in a single line, used for small pieces of logic like custom sorting rules. It's a convenient shortcut for one-off tasks, but for anything more complex, developers write a regular named function instead for clarity.”
Code Example
people = [{"name": "Ada", "age": 34}, {"name": "Bo", "age": 22}]
# Sort by age using a lambda as the key function
by_age = sorted(people, key=lambda p: p["age"])
print([p["name"] for p in by_age]) # ['Bo', 'Ada']
nums = [1, -2, 3, -4, 5]
doubled = list(map(lambda x: x * 2, nums))
positives = list(filter(lambda x: x > 0, nums))
print(doubled) # [2, -4, 6, -8, 10]
print(positives) # [1, 3, 5]Follow-up Questions
- Why can't a lambda contain multiple statements?
- When would you prefer a def function over a lambda?
- How do lambdas capture variables from an enclosing scope?
- Can a lambda have default argument values?
- What is the common late-binding closure bug with lambdas in a loop?
MCQ Practice
1. What does lambda x, y: x + y evaluate to when called with (2, 3)?
The lambda takes x and y and returns their sum implicitly; calling it with (2, 3) yields 5.
2. Which of these is a valid lambda body?
Lambda bodies must be a single expression; loops, assignments, and multi-statement bodies are not allowed.
3. What is the most idiomatic use of a lambda in standard library code?
Lambdas are most commonly used as short inline key functions for sorted(), min(), and max().
Flash Cards
Lambda function syntax? — lambda arguments: expression
Can a lambda body contain multiple statements? — No — only a single expression is allowed.
Most idiomatic lambda use case? — As the key= function for sorted(), min(), or max().
When should you avoid a lambda? — When the logic is more than a trivial expression — use a named def function instead.