TGViewer
Data Science & Machine Learning Data Science & Machine Learning @datasciencefun ยท 77.8K subscribers
Post #4473 2.67K
๐Ÿš€ 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 available in the functools module.

from functools import reduce
numbers = [1, 2, 3, 4]
result = reduce(lambda a, b: a + b, numbers)
print(result)


Output: 10

๐Ÿ”น 8. Difference Between map(), filter(), and reduce()

map(): Transforms every element in an iterable and returns a new iterable.

filter(): Keeps only elements that match a condition and returns a filtered iterable.

reduce(): Combines all elements into a single value.

๐Ÿ”น 9. Real-World Data Science Example

Suppose you have customer purchase amounts.

purchases = [1200, 450, 1800, 900, 2500]
high_value = list(filter(lambda x: x > 1000, purchases))
print(high_value)


Output: [1200, 1800, 2500]

Now calculate the total revenue.

from functools import reduce
total = reduce(lambda a, b: a + b, purchases)
print(total)


Output: 6850

๐Ÿ”น 10. Combining map() and filter()

numbers = [1, 2, 3, 4, 5, 6]
result = list(
map(
lambda x: x * 10,
filter(lambda x: x % 2 == 0, numbers)
)
)
print(result)
  • โค 5
More from @datasciencefun
  1. Oct 7, 2026ORDER BY salary DESC LIMIT 5; Mistake 3 โ€” Forgetting that LIMIT applies after sorting Forโ€ฆ
  2. Oct 7, 2026๐Ÿš€ Data Science Roadmap 2026 ๐Ÿ“ Phase 3: SQL for Data Science ๐Ÿ“– Topic 4 โ€” LIMIT LIMIT isโ€ฆ
  3. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  4. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  5. Oct 2, 2026Data Visualisation tips for beginners
  6. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’