๐ Python Lambda Function Explained – Easy Guide for Students & Professionals
Lambda functions in Python are small, powerful, and often misunderstood — but once you get the hang of them, they become a valuable tool in your coding toolkit.
In this guide, we’ll explain what lambda functions are, when to use them, how to write them, and some real-world examples. Whether you’re a beginner or a working professional, this article will help you master Python’s anonymous functions.
✅ What is a Lambda Function in Python?
A lambda function is a small, anonymous function defined using the keyword lambda
.
It is:
-
Used for short, one-line functions
-
Often used where a function is needed temporarily
-
Can take any number of arguments but has only one expression
๐ง Syntax of Lambda Function
lambda arguments: expression
This returns a function object that you can call just like a normal function.
๐งช Basic Example
Let’s write a lambda function that adds 10 to a number:
add_ten = lambda x: x + 10
print(add_ten(5)) # Output: 15
Here, lambda x: x + 10
is equivalent to:
def add_ten(x):
return x + 10
๐ Multiple Arguments Example
multiply = lambda x, y: x * y
print(multiply(3, 4)) # Output: 12
⚙️ Where to Use Lambda Functions
Lambda functions are most commonly used:
-
Inside
map()
,filter()
, andreduce()
-
When passing a simple function as an argument
-
To simplify short logic without cluttering the code
๐งฉ Lambda with map()
numbers = [1, 2, 3, 4]
squares = list(map(lambda x: x ** 2, numbers))
print(squares) # Output: [1, 4, 9, 16]
๐งช Lambda with filter()
nums = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, nums))
print(evens) # Output: [2, 4, 6]
➕ Lambda with sorted()
students = [('Alice', 85), ('Bob', 72), ('Charlie', 90)]
# Sort by score (second element)
sorted_students = sorted(students, key=lambda x: x[1])
print(sorted_students)
# Output: [('Bob', 72), ('Alice', 85), ('Charlie', 90)]
⚠️ When NOT to Use Lambda
-
For complex logic (use
def
instead) -
When readability is more important than brevity
-
If the function needs a docstring or name
๐ Summary Table
Feature | Lambda Function | Normal Function (def ) |
---|---|---|
Syntax | One-liner | Multi-line allowed |
Naming | Anonymous | Named |
Usage | Quick, temporary functions | Reusable and readable |
Supports Docstring | ❌ | ✅ |
Used with | map(), filter(), sort(), etc. | Anywhere |
๐ Practice Task for Students
Try writing a lambda function that checks if a number is even or odd:
is_even = lambda x: "Even" if x % 2 == 0 else "Odd"
print(is_even(7)) # Output: Odd
๐งฉ Real-World Use Case: Sorting Dictionary by Value
data = {'a': 5, 'b': 2, 'c': 9}
sorted_data = sorted(data.items(), key=lambda item: item[1])
print(sorted_data) # Output: [('b', 2), ('a', 5), ('c', 9)]
✅ Conclusion
Lambda functions are an elegant way to write quick, throwaway functions without creating full function definitions. For students, they help understand functional programming. For professionals, they help write cleaner, faster, and more Pythonic code.
When used wisely, lambda functions can improve your code efficiency — just remember to keep them short and readable!
watch video to understand Python Lambda Function below:
No comments:
Post a Comment