map() and filter()
Two powerful built-ins — map() applies a function to every item, filter() keeps only items that pass a test.
"map() is a conveyor belt — every item goes through a machine and comes out transformed. filter() is a sieve — only items that fit the holes pass through."
— ShurAIThe Big Picture
func to every item.Returns a new collection of the same length with each item transformed.
func returns True.Returns a smaller collection — items that failed the test are gone.
map() — Transform Every Item
Pass a function and a list. Python applies the function to each item and gives back the results. Wrap in list() to see the output:
nums = [1, 2, 3, 4, 5]
# Square every number
squared = list(map(lambda x: x**2, nums))
print(squared) # [1, 4, 9, 16, 25]
# Convert strings to integers
raw = ["10", "20", "30"]
ints = list(map(int, raw))
print(ints) # [10, 20, 30]
# Uppercase every name
names = ["riya", "arjun", "sneha"]
upper = list(map(str.upper, names))
print(upper) # ['RIYA', 'ARJUN', 'SNEHA']
filter() — Keep Only Matching Items
Pass a function that returns True or False. Python keeps only the items where it returned True:
nums = [1, 2, 3, 4, 5, 6, 7, 8]
# Keep only even numbers
evens = list(filter(lambda x: x % 2 == 0, nums))
print(evens) # [2, 4, 6, 8]
# Keep only passing scores (>= 70)
scores = [45, 82, 61, 90, 55, 77]
passing = list(filter(lambda s: s >= 70, scores))
print(passing) # [82, 90, 77]
# Keep only non-empty strings
data = ["Riya", "", "Arjun", "", "Sneha"]
clean = list(filter(None, data)) # None keeps all truthy items
print(clean) # ['Riya', 'Arjun', 'Sneha']
Chaining map() and filter()
You can chain them — filter first, then map the result:
scores = [45, 82, 61, 90, 55, 77]
# Step 1: keep only passing scores
# Step 2: add 5 bonus points to each
boosted = list(
map(lambda s: s + 5,
filter(lambda s: s >= 70, scores))
)
print(boosted) # [87, 95, 82]
map() vs filter() vs List Comprehension
| Tool | Use for | Example |
|---|---|---|
| map() | Transform every item | map(int, ["1","2"]) |
| filter() | Keep matching items only | filter(lambda x: x>0, nums) |
| [ ... ] | Transform + filter together, most readable | [x*2 for x in nums if x>0] |
Real Example — Process a Shopping List
items = [
("Rice", 280, True), # (name, price, in_stock)
("Dal", 120, False),
("Milk", 60, True),
("Eggs", 96, True),
("Oil", 210, False),
]
# Keep only in-stock items
available = list(filter(lambda x: x[2], items))
# Apply 5% discount to prices
discounted = list(map(
lambda x: (x[0], round(x[1] * 0.95), x[2]),
available
))
print("In-stock with 5% discount:")
for name, price, _ in discounted:
print(f" {name:8} Rs.{price}")
In-stock with 5% discount:
Rice Rs.266
Milk Rs.57
Eggs Rs.91
"map() and filter() are elegant when paired with lambda. For anything more complex, a list comprehension is usually clearer."
— ShurAI🧠 Quiz — Q1
What does list(map(lambda x: x*2, [1,2,3])) return?
🧠 Quiz — Q2
What does filter() do to items where the function returns False?
🧠 Quiz — Q3
Why do you wrap map() and filter() in list()?
🧠 Quiz — Q4
Which is the best tool when you need to BOTH filter AND transform items in one readable line?