Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · Python · 04 Jul 2026 · Hindi + English

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?

A lambda is a small anonymous (nameless) function written in one line:
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
25

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"))
30 72 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)
[90, 120, 150, 180]

filter() — keep items that pass a test:

marks = [25, 60, 33, 90, 18]
passed = list(filter(lambda m: m >= 33, marks))
print(passed)
[60, 33, 90]

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)
[('Rahul', 95), ('Aman', 92), ('Priya', 88)]

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 def function 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

Pointlambdadef function
NameAnonymousNamed
BodyOne expression onlyAny number of statements
returnAutomaticWritten explicitly
Best forShort throwaway logic in map/filter/sortedEverything else

Lambda function क्या है?

Lambda एक छोटा anonymous (बिना नाम का) one-line function है:
lambda arguments: expression
यह कितने भी arguments ले सकता है लेकिन सिर्फ एक expression, जिसका result automatically return होता है.
square = lambda x: x * x
print(square(5))

# बिल्कुल यही:
def square(x):
    return x * x
25

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"))
30 72 Gagan Sharma

Lambda जहां असली काम आता है: map(), filter(), sorted()

map() — हर item पर apply:

marks = [45, 60, 75, 90]
doubled = list(map(lambda m: m * 2, marks))
print(doubled)
[90, 120, 150, 180]

filter() — test pass करने वाले items रखें:

marks = [25, 60, 33, 90, 18]
passed = list(filter(lambda m: m >= 33, marks))
print(passed)
[60, 33, 90]

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)
[('Rahul', 95), ('Aman', 92), ('Priya', 88)]

Lambda कब NOT use करें

  • Multiple statements नहीं: lambda में if-blocks, loops या कई lines नहीं आ सकतीं — सिर्फ एक expression.
  • Complex logic: अगर पढ़ने के लिए दो बार सोचना पड़े, तो नाम वाला normal def function लिखें.
  • Reuse: same logic 3 जगह चाहिए तो named function cleaner है. PEP 8 भी real projects में lambda को variable में assign न करने की सलाह देता है.

Interview summary

Pointlambdadef function
NameAnonymousNamed
Bodyसिर्फ एक expressionकितने भी statements
returnAutomaticExplicitly लिखना होता है
Best formap/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.