map() and filter()
map() and filter() are built-in functions that run one function over every item in a list: map() keeps a result for each item, and filter() keeps only the items that pass a test.
Last updated: 13 Oct, 2026 · Python 3.12
In Functions as values you handed a function to sorted with key=. map and filter take a function the same way and run it across a whole list for you, with no loop to write.
print() to see it in a file, as the run below does. The clip is an older one and writes its even check as if num % 2 == 0: return True, which gives back None for odd numbers. That still works inside filter, since None counts as false, and the page uses the current idiom, return num % 2 == 0, later.def even(num):
if num % 2 == 0:
return True
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
print(list(filter(even, numbers)))
print(list(map(lambda x: x * x, numbers)))[2, 4, 6, 8] [1, 4, 9, 16, 25, 36, 49, 64]
filter kept the even numbers and dropped the rest. map squared every number and kept them all. That is the contrast: filter chooses which items stay, map changes each item and keeps the whole list. Both give back a lazy object, so list(...) is wrapped around them to see the values. The page runs the same two functions over tickets below.
Syntax:
map(function, items) # one result for every item
filter(function, items) # only the items the function says are True
list(map(...)) # both are lazy; wrap in list() to read themTransforming every item with map()
map calls a function on each item and collects the results. The function can be a method name like str.strip or a built-in like len; it does not need brackets, because map does the calling.
tickets = [" Refund please ", "Where is my ORDER?", " cancelled "]
clean = list(map(str.strip, tickets))
print(clean)
lengths = list(map(len, clean))
print(lengths)['Refund please', 'Where is my ORDER?', 'cancelled'] [13, 18, 9]
str.strip ran on each ticket and removed the spaces at both ends. The second map then ran len on each cleaned ticket. Each call gives one list back, the same length as the one that went in.
Combining two lists with map()
When a function takes two arguments, map can read from two lists at once, taking one item from each. The video uses this to add two lists together.
prompts = [40, 55, 30]
replies = [12, 20, 8]
totals = list(map(lambda p, r: p + r, prompts, replies))
print(totals)[52, 75, 38]
The lambda takes two values, one from prompts and one from replies, and adds them. Here each total is the tokens a model read plus the tokens it wrote for one call.
Keeping some items with filter()
filter calls a function that answers True or False and keeps only the items that answer True.
scores = [0.2, 0.9, 0.55, 0.95, 0.4]
confident = list(filter(lambda s: s >= 0.5, scores))
print(confident)[0.9, 0.55, 0.95]
The lambda is the test: keep a score of 0.5 or more. filter ran it on each score and kept the three that passed, in their original order. This is how you drop a model's low-confidence answers before acting on them.
map() vs filter()
map | filter | |
|---|---|---|
| The function returns | A new value for each item | True or False |
| Items kept | All of them | Only those that pass |
| Result length | Same as the input | The input, or shorter |
| Used for | Changing every item | Choosing which items stay |
Where map and filter show up in AI code
- Cleaning or shortening every text in a batch before it goes to a model, with
map. - Dropping the answers that fail a check, with
filter. - A comprehension does both jobs in one line and reads better for most people;
mapandfilterare worth knowing because framework code uses them.
map and filter give back a lazy object, not a list, and it empties once you read it. Wrap it in list() or loop over it once. There is no built-in reduce; it lives in functools, and the Complete Python series does not cover it.Related
- Previous: Functions as values
- Next: import and modules
- Reference: map() in the Python docs
- Use
mapwithstr.upperto shout a list of three ticket texts. - Change the
filtertest to keep scores above 0.9, and run it again. - Rewrite the scores filter as a list comprehension and check it gives the same list.
Little by little, you're building something great.