🚀 Welcome back to our AI Engineer Roadmap! ❤️
In the previous posts, we learned about functions and solved some tricky function-based MCQs. Now let's move to the next topic in Python fundamentals.
📖 Phase 1: Programming Fundamentals
📌 Topic 12: Lambda Functions
A Lambda Function is a small, anonymous function that can be written in a single line.
Unlike regular functions created using def, lambda functions are created using the lambda keyword.
Why Do We Need Lambda Functions?
Lambda functions are useful when:
• You need a small function for a short task
• You don't want to define a full function using def
• You need a function temporarily
• You're working with functions like map(), filter(), and sorted()
1. Creating a Lambda Function
A normal function:
def square(x):
return x * x
The same function using lambda:
square = lambda x: x * x
print(square(5))
Output: 25
Lambda Syntax
lambda arguments: expression
For example: lambda x: x + 10
• lambda → Keyword used to create the function
• x → Argument
• x + 10 → Expression that is returned
2. Lambda with Multiple Arguments
A lambda function can accept multiple arguments.
add = lambda a, b: a + b
print(add(10, 20))
Output: 30
multiply = lambda x, y: x * y
print(multiply(5, 4))
Output: 20
3. Lambda with if-else
Lambda functions can also contain conditional expressions.
check = lambda x: "Even" if x % 2 == 0 else "Odd"
print(check(10))
print(check(7))
Output:
Even
Odd
4. Lambda with map()
map() applies a function to every item in an iterable.
numbers = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x * x, numbers))
print(squares)
Output: [1, 4, 9, 16, 25]
5. Lambda with filter()
filter() selects elements based on a condition.
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)
Output: [2, 4, 6]
6. Lambda with sorted()
Lambda functions are very useful when sorting complex data.
Example:
students = [
("Rahul", 80),
("Priya", 95),
("Amit", 70)
]
students.sort(key=lambda x: x[1])
print(students)
Output: [('Amit', 70), ('Rahul', 80), ('Priya', 95)]
Here, lambda x: x[1] tells Python to sort using the second element of each tuple.
Lambda vs Regular Function
• Regular function:
def square(x):
return x * x
• Lambda function:
square = lambda x: x * x
Both produce the same result.
When Should You Use Lambda?
Use lambda when:
✅ The function is very small
✅ The operation is simple
✅ You need the function temporarily
✅ You're working with map(), filter(), or sorted()
Avoid lambda when:
❌ The logic becomes complicated
❌ The function needs multiple statements
❌ A meaningful function name and documentation would improve readability
In those situations, a regular def function is usually better.
Real-World AI/Data Example
Lambda functions are commonly used while preprocessing data.
scores = [45, 67, 82, 91, 38]
updated_scores = list(map(lambda x: x / 100, scores))
print(updated_scores)
Output: [0.45, 0.67, 0.82, 0.91, 0.38]
This kind of transformation can be useful when preparing data before feeding it into a Machine Learning model.
Common Beginner Mistakes
❌ Trying to put complex logic into a lambda
❌ Forgetting that a lambda automatically returns its expression
❌ Confusing map() and filter()
Key Takeaways
• Lambda functions are small anonymous functions
• They are created using the lambda keyword
• They can accept multiple arguments
• They return the result of a single expression
• They're especially useful with map(), filter(), and sorted()
• For complex logic, prefer a regular def function
➡️ Double Tap ❤️ For More