Python - Lambda Expressions
In Python, we often define functions using the def keyword. But sometimes we need a small, quick function that is used only once.
For this purpose, Python provides Lambda Expressions (also called anonymous functions).
In this tutorial, you will learn what lambda expressions are, how they work, and when to use them.
What is a Lambda Expression?
A lambda expression is:
A small anonymous function defined using the
lambdakeyword.
It can have any number of inputs but only one expression.
Syntax
lambda arguments: expressionSimple Example
add = lambda a, b: a + b
print(add(5, 3))Output
8Lambda vs Normal Function
Normal Function
def add(a, b):
return a + bLambda Function
add = lambda a, b: a + bWhy Use Lambda Functions?
Lambda functions are useful for:
- Short, simple operations
- One-time use functions
- Functional programming (map, filter, reduce)
- Cleaner and shorter code
Lambda with map()
The map() function applies a function to all items in a list.
Example
numbers = [1, 2, 3, 4]
result = list(map(lambda x: x * 2, numbers))
print(result)Output
[2, 4, 6, 8]Lambda with filter()
The filter() function filters items based on condition.
Example
numbers = [1, 2, 3, 4, 5, 6]
result = list(filter(lambda x: x % 2 == 0, numbers))
print(result)Output
[2, 4, 6]Lambda with sorted()
You can use lambda for custom sorting.
Example
students = [("Alice", 20), ("Bob", 18), ("Charlie", 22)]
sorted_students = sorted(students, key=lambda x: x[1])
print(sorted_students)Output
[('Bob', 18), ('Alice', 20), ('Charlie', 22)]Lambda with Multiple Arguments
multiply = lambda a, b, c: a * b * c
print(multiply(2, 3, 4))Output
24Lambda in Real-World Use
Sorting Dictionary
students = [
{"name": "A", "grade": 85},
{"name": "B", "grade": 90},
{"name": "C", "grade": 80}
]
sorted_list = sorted(students, key=lambda x: x["grade"])
print(sorted_list)Lambda with reduce()
from functools import reduce
numbers = [1, 2, 3, 4]
result = reduce(lambda x, y: x + y, numbers)
print(result)Output
10When to Use Lambda Functions
✔ Short operations
✔ One-time use functions
✔ map(), filter(), sorted()
✔ Functional programming style
When NOT to Use Lambda
❌ Complex logic
❌ Multiple lines of code
❌ Readability is important
❌ Debugging-heavy functions
Lambda vs def Function
| Feature | def Function | Lambda |
|---|---|---|
| Name | Has name | Anonymous |
| Complexity | Complex logic | Simple only |
| Lines | Multiple | Single line |
| Readability | High | Medium |
Advantages of Lambda
- Short and concise
- Quick to write
- Useful in functional programming
- Improves readability for simple tasks
Disadvantages
- Limited to one expression
- Hard to debug
- Not suitable for complex logic
Common Mistakes
1. Using lambda for complex logic
2. Forgetting it returns expression only
3. Overusing lambda functions
Best Practices
1. Use lambda for simple operations
2. Use def for complex logic
3. Combine with map/filter/sorted
4. Keep expressions readable
Real-World Example: Discount Calculation
prices = [100, 200, 300]
discounted = list(map(lambda x: x * 0.9, prices))
print(discounted)Summary
Lambda expressions in Python are small anonymous functions used for simple operations. They are commonly used with functions like map(), filter(), and sorted().
Key Takeaways
- Defined using
lambda - Single expression only
- Useful for short functions
- Common in functional programming
- Best for quick, simple logic
Mastering lambda expressions helps you write cleaner and more efficient Python code.


0 Comments