Lambda Function in Python: Syntax, Examples and When to Use
A lambda is a one-line anonymous function: lambda arguments: expression. See practical examples with map(), filter(), sorted() and when NOT to use lambda.
What is a lambda function?
lambda arguments: expression
It can take any number of arguments but only one expression, whose result is automatically returned.square = lambda x: x * x
print(square(5))
# exactly the same as:
def square(x):
return x * x
Lambda with multiple arguments
add = lambda a, b: a + b
maximum = lambda a, b: a if a > b else b
fullname = lambda first, last: first + " " + last
print(add(10, 20))
print(maximum(45, 72))
print(fullname("Gagan", "Sharma"))
Where lambda actually shines: map(), filter(), sorted()
map() — apply to every item:
marks = [45, 60, 75, 90]
percent = list(map(lambda m: m / 100 * 100, marks))
doubled = list(map(lambda m: m * 2, marks))
print(doubled)
filter() — keep items that pass a test:
marks = [25, 60, 33, 90, 18]
passed = list(filter(lambda m: m >= 33, marks))
print(passed)
sorted() with key — the most common real-world use:
students = [("Aman", 92), ("Priya", 88), ("Rahul", 95)]
by_marks = sorted(students, key=lambda s: s[1], reverse=True)
print(by_marks)
When NOT to use lambda
- No multiple statements: lambda cannot contain if-blocks, loops, or several lines — only one expression.
- Complex logic: if you need to think twice to read it, write a normal
deffunction with a name. - Reuse: if the same logic is needed in 3 places, a named function is cleaner. PEP 8 even recommends not assigning lambdas to variables in real projects.
Interview summary
| Point | lambda | def function |
|---|---|---|
| Name | Anonymous | Named |
| Body | One expression only | Any number of statements |
| return | Automatic | Written explicitly |
| Best for | Short throwaway logic in map/filter/sorted | Everything else |
Lambda function क्या है?
lambda arguments: expression
यह कितने भी arguments ले सकता है लेकिन सिर्फ एक expression, जिसका result automatically return होता है.square = lambda x: x * x
print(square(5))
# बिल्कुल यही:
def square(x):
return x * x
Multiple arguments के साथ Lambda
add = lambda a, b: a + b
maximum = lambda a, b: a if a > b else b
fullname = lambda first, last: first + " " + last
print(add(10, 20))
print(maximum(45, 72))
print(fullname("Gagan", "Sharma"))
Lambda जहां असली काम आता है: map(), filter(), sorted()
map() — हर item पर apply:
marks = [45, 60, 75, 90]
doubled = list(map(lambda m: m * 2, marks))
print(doubled)
filter() — test pass करने वाले items रखें:
marks = [25, 60, 33, 90, 18]
passed = list(filter(lambda m: m >= 33, marks))
print(passed)
sorted() with key — सबसे common real-world use:
students = [("Aman", 92), ("Priya", 88), ("Rahul", 95)]
by_marks = sorted(students, key=lambda s: s[1], reverse=True)
print(by_marks)
Lambda कब NOT use करें
- Multiple statements नहीं: lambda में if-blocks, loops या कई lines नहीं आ सकतीं — सिर्फ एक expression.
- Complex logic: अगर पढ़ने के लिए दो बार सोचना पड़े, तो नाम वाला normal
deffunction लिखें. - Reuse: same logic 3 जगह चाहिए तो named function cleaner है. PEP 8 भी real projects में lambda को variable में assign न करने की सलाह देता है.
Interview summary
| Point | lambda | def function |
|---|---|---|
| Name | Anonymous | Named |
| Body | सिर्फ एक expression | कितने भी statements |
| return | Automatic | Explicitly लिखना होता है |
| Best for | map/filter/sorted में short logic | बाकी सब कुछ |
Frequently Asked Questions
What is a lambda function in Python?
A lambda is an anonymous one-line function with the syntax lambda arguments: expression, where the expression result is returned automatically.
Can a lambda have multiple statements?
No. A lambda body must be a single expression — no loops, no multiple lines. For anything more, use a normal def function.
Where is lambda most used in real code?
As the key argument of sorted(), min() and max(), and inside map() and filter() for short one-time transformations.