Code of the Day
BeginnerFundamental Patterns

Filter and transform

Build new sequences by keeping only items that pass a test, or by changing every item the same way.

CS FundamentalsBeginner9 min read
Recommended first
By the end of this lesson you will be able to:
  • Implement a filter that collects items matching a condition into a new list
  • Implement a map/transform that applies the same operation to every item
  • Combine filter and transform in a single pass

The previous lesson was about examining a sequence — finding items or counting them. This lesson is about producing a new sequence from an existing one.

Two patterns do this:

  • Filter — keep only the items that pass a test; discard the rest.
  • Transform (sometimes called map) — apply the same operation to every item, creating a new list of modified values.

Both patterns return a new list. The original list is left untouched.

The accumulate pattern

Both filter and transform rely on the same underlying idea: start with an empty list, loop through the original, and selectively append to the new list.

result = []          # start empty
for item in source:
    # decide whether (filter) or what (transform) to append
    result.append(...)
return result

This is the accumulate pattern. It's worth recognising by name because you'll reach for it any time you need to produce a new sequence.

Filter: keep what passes the test

def keep_evens(nums):
    result = []
    for n in nums:
        if n % 2 == 0:
            result.append(n)
    return result

The condition if n % 2 == 0 is the gate. Only even numbers pass through to result. Odd numbers are quietly skipped.

Transform: change every item the same way

def double_all(nums):
    result = []
    for n in nums:
        result.append(n * 2)
    return result

There's no condition — every item is transformed. The new list has the same number of elements as the original, just with each value doubled.

Python has built-in tools for both patterns: filter(), map(), and list comprehensions. Building these by hand first makes clear what those tools are actually doing — once you understand the loop, the shorthand is just syntax, not magic.

Combining filter and transform in one pass

Often you want to keep some items and change them. You can do both in a single loop — filter first, then transform (or the other way around, depending on which is cheaper to compute).

Python — editable, runs in your browser

Run it, then try adjusting the condition in double_positives — for example, keep only numbers greater than 2 and triple them instead.

The difference from find and count

  • Find and count leave the list alone; they return a number or a single value.
  • Filter and transform produce a new list.

When a problem says "return a list of …", that's your signal to reach for the accumulate pattern. When it says "how many …" or "is there a …", reach for count or find.

Short wordsPython

Write short_words(words, max_len) that returns a new list containing only words whose length is <= max_len.

short_words(["hi", "hello", "hey"], 3)['hi', 'hey']

Where to go next

Next: nested loops — what to do when a single pass through the list isn't enough and you need to compare items against each other.

Finished reading? Mark it complete to track your progress.

On this page