📘 Lesson · Lesson 81
Map, Filter, Reduce
Map, Filter, Reduce
Functional Tools
💡 At a Glance
map, filter and reduce apply a function to a whole list without writing loops — often used with lambda.
map() — transform each
Python
nums = [1, 2, 3, 4] squared = list(map(lambda x: x*x, nums)) print(squared)
[1, 4, 9, 16]
filter() — keep some
Python
nums = [1, 2, 3, 4, 5, 6] even = list(filter(lambda x: x % 2 == 0, nums)) print(even)
[2, 4, 6]
reduce() — combine to one
Python
from functools import reduce nums = [1, 2, 3, 4] total = reduce(lambda a, b: a + b, nums) print(total)
10
Summary
- map transforms each item, filter keeps matching items, reduce combines to one value.
- Often paired with lambda for short functions.
Functional Tools
💡 एक नज़र में
map, filter और reduce पूरी list पर function लगाते हैं बिना loops लिखे — अक्सर lambda के साथ।
map() — हर एक transform
Python
nums = [1, 2, 3, 4] squared = list(map(lambda x: x*x, nums)) print(squared)
[1, 4, 9, 16]
filter() — कुछ रखें
Python
nums = [1, 2, 3, 4, 5, 6] even = list(filter(lambda x: x % 2 == 0, nums)) print(even)
[2, 4, 6]
reduce() — एक में मिलाएं
Python
from functools import reduce nums = [1, 2, 3, 4] total = reduce(lambda a, b: a + b, nums) print(total)
10
सारांश
- map हर item transform, filter matching रखता, reduce एक value में मिलाता।
- छोटे functions के लिए अक्सर lambda के साथ।