Lambda, map, filter and reduce
Lambda, map, filter और reduce
What is a Lambda?
A lambda is a tiny, one-line function with no name (anonymous). It is handy for short tasks you do not want to write a full def for.
Lambda Syntax
# Normal function
def square(x):
return x * x
# Same thing as a lambda
square = lambda x: x * x
print(square(5))Pattern: lambda arguments: expression. The expression's value is returned automatically.
Program 1: Simple Lambda
add = lambda a, b: a + b is_even = lambda n: n % 2 == 0 print(add(3, 4)) print(is_even(10))
Program 2: Lambda with map()
map() applies a function to every item in a list.
nums = [1, 2, 3, 4] squares = list(map(lambda x: x * x, nums)) print(squares)
The lambda squares each number; map runs it across the whole list.
Program 3: Lambda with filter()
filter() keeps only items where the function returns True.
nums = [5, 12, 8, 21, 3] big = list(filter(lambda x: x > 10, nums)) print(big)
Program 4: Lambda with sorted()
A lambda can tell sorted() what to sort by.
students = [("Aman", 88), ("Riya", 95), ("Karan", 79)]
# sort by marks (the second item)
top = sorted(students, key=lambda s: s[1], reverse=True)
print(top)key=lambda s: s[1] tells sorted to use the marks for ordering.
When to Use Lambda
- For short, throwaway functions used once.
- As the
keyinsorted,map,filter. - For anything longer than one expression, use a normal
definstead.
Common Mistakes
- Trying to put multiple statements in a lambda — only one expression is allowed.
- Forgetting
list()aroundmap/filter(they return objects, not lists).
Practice Tasks
- Write a lambda that cubes a number.
- Use
mapwith a lambda to double every item in a list. - Use
filterto keep only words longer than 4 letters. - Sort a list of names by their length using a lambda.
Summary
- Lambda is a one-line anonymous function:
lambda args: expression. - Common with
map,filter,sorted. - Use a normal
deffor anything longer.
Lambda क्या है?
Lambda एक छोटा, एक-line वाला बिना नाम (anonymous) function है। छोटे कामों के लिए सुविधाजनक जिनके लिए पूरा def नहीं लिखना चाहते।
Lambda Syntax
# Normal function
def square(x):
return x * x
# वही चीज़ lambda के रूप में
square = lambda x: x * x
print(square(5))Pattern: lambda arguments: expression। expression की value अपने आप return होती है।
Program 1: Simple Lambda
add = lambda a, b: a + b is_even = lambda n: n % 2 == 0 print(add(3, 4)) print(is_even(10))
Program 2: map() के साथ Lambda
map() किसी function को list के हर item पर लगाता है।
nums = [1, 2, 3, 4] squares = list(map(lambda x: x * x, nums)) print(squares)
Lambda हर number का square करता है; map इसे पूरी list पर चलाता है।
Program 3: filter() के साथ Lambda
filter() सिर्फ वे items रखता है जहाँ function True लौटाए।
nums = [5, 12, 8, 21, 3] big = list(filter(lambda x: x > 10, nums)) print(big)
Program 4: sorted() के साथ Lambda
Lambda sorted() को बता सकता है कि किस आधार पर sort करना है।
students = [("Aman", 88), ("Riya", 95), ("Karan", 79)]
# marks (दूसरे item) से sort
top = sorted(students, key=lambda s: s[1], reverse=True)
print(top)key=lambda s: s[1] sorted को marks से order करने को कहता है।
Lambda कब Use करें
- छोटे, एक बार use होने वाले functions के लिए।
sorted,map,filterमेंkeyके रूप में।- एक expression से लंबा कुछ भी हो तो normal
defuse करें।
सामान्य गलतियाँ
- lambda में कई statements डालने की कोशिश — सिर्फ एक expression allowed है।
map/filterके aroundlist()भूलना (वे objects लौटाते हैं, lists नहीं)।
Practice Tasks
- एक lambda लिखें जो number का cube करे।
mapऔर lambda से list के हर item को double करें।filterसे सिर्फ 4 अक्षर से लंबे words रखें।- Lambda से names की list उनकी length के आधार पर sort करें।
सारांश
- Lambda एक-line anonymous function है:
lambda args: expression। map,filter,sortedके साथ common।- लंबे काम के लिए normal
defuse करें।