🚀 Data Science Roadmap 2026
📘 Phase 1: Programming Fundamentals
🐍 Topic 9: Python Lambda Functions, map(), filter(), and reduce()
Welcome back! 👋
So far, you've learned Python basics, loops, functions, data structures, and list comprehensions. In this lesson, you'll learn functional programming concepts in Python using Lambda Functions, map(), filter(), and reduce().
These are widely used in Data Science for transforming, filtering, and processing large datasets efficiently.
🔹 1. What is a Lambda Function?
A Lambda Function is a small anonymous function that can have any number of arguments but only one expression.
Unlike normal functions, lambda functions don't require a name.
Syntax
lambda arguments: expression
Example
square = lambda x: x * x
print(square(5))
Output: 25
This is equivalent to:
def square(x):
return x * x
🔹 2. Why Use Lambda Functions?
Lambda functions are useful when:
✅ You need a simple function only once.
✅ You want shorter, cleaner code.
✅ You're using functions like map(), filter(), or sorted().
🔹 3. Lambda with Multiple Arguments
add = lambda a, b: a + b
print(add(10, 20))
Output: 30
🔹 4. The map() Function
The map() function applies a function to every item in an iterable.
Syntax: map(function, iterable)
Example
numbers = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x ** 2, numbers))
print(squares)
Output: [1, 4, 9, 16, 25]
🔹 5. Using map() with a Normal Function
def double(x):
return x * 2
numbers = [1, 2, 3, 4]
result = list(map(double, numbers))
print(result)
Output: [2, 4, 6, 8]
🔹 6. The filter() Function
The filter() function selects only those elements that satisfy a condition.
Syntax: filter(function, iterable)
Example
numbers = [1, 2, 3, 4, 5, 6]
even = list(filter(lambda x: x % 2 == 0, numbers))
print(even)
Output: [2, 4, 6]
🔹 7. The reduce() Function
The reduce() function applies a function repeatedly to reduce an iterable to a single value.
It is ava…
Read in full →