Python for AIPython 3.12 · Pydantic 2.12
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
43 small wins to finish your pathNext lesson →

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.

Contrasting map() and filter() · from the Complete Python series · 0:47 to 3:31
The video runs each line in a Jupyter notebook; wrap a line in 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.
ExampleFrom the video, run here
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)))

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:

python
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 them

Transforming 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.

Example
tickets = ["  Refund please  ", "Where is my ORDER?", " cancelled "]
clean = list(map(str.strip, tickets))
print(clean)
lengths = list(map(len, clean))
print(lengths)

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.

Example
prompts = [40, 55, 30]
replies = [12, 20, 8]
totals = list(map(lambda p, r: p + r, prompts, replies))
print(totals)

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.

Example
scores = [0.2, 0.9, 0.55, 0.95, 0.4]
confident = list(filter(lambda s: s >= 0.5, scores))
print(confident)

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()

mapfilter
The function returnsA new value for each itemTrue or False
Items keptAll of themOnly those that pass
Result lengthSame as the inputThe input, or shorter
Used forChanging every itemChoosing 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; map and filter are worth knowing because framework code uses them.
Watch out. 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.
Try it yourself
  • Use map with str.upper to shout a list of three ticket texts.
  • Change the filter test 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.