runtime
56 / 250
lesson 56 Collection Algorithms · intermediate

Keep the records that match

A filter walks a collection and builds a new list of only the items that pass a test. The original stays complete.

You'll be able to
  • Explain why a filter builds a new list instead of changing the one it reads
  • Write a function that returns the matching records, or one field from each
  • Combine two conditions in the test and keep the original order
The idea

Most data is things you don't need right now. Out of a day's orders you want only the returned ones; out of a gradebook, only the students who passed. A filter answers *which of these pass the test?* and gives the answer back as a new list.

The pattern has three parts: start an empty result list, walk every item with for, and append an item only inside an if. The list you read is never changed, so the rest of the program can still use all of it.

Run each example to see what it prints. Running examples doesn't count toward progress.

example-1.py · Totals of 50 or more
totals = [35.84, 91.47, 22.35, 246.6, 54.55]
large = []
for total in totals:
    if total >= 50:
        large.append(total)
print(large)
print(len(totals), "orders,", len(large), "of them 50 or more")

In a real program the filter lives in a function: the data and the threshold come in as parameters, and the new list goes back with return. The return sits after the loop, at the function's own indentation, so it runs once every item has been tested.

The test and the kept value don't have to be the same thing. These gradebook rows are dictionaries: the filter tests score and keeps name.

example-2.py · Who passed
def passed(students, cutoff):
    names = []
    for student in students:
        if student["score"] >= cutoff:
            names.append(student["name"])
    return names

gradebook = [
    {"name": "Ana", "score": 72},
    {"name": "Ben", "score": 58},
    {"name": "Chen", "score": 91},
    {"name": "Dara", "score": 60},
]
print(passed(gradebook, 60))
print(passed(gradebook, 95))

When to use it: whenever the question is *which ones?* (cancelled orders, readings below freezing, customers in one city). Mistakes to avoid: indenting return into the loop, so the function stops after the first item; and testing one field while appending another by accident. The concept check below shows the first one running.

Review
  • A filter builds a new list: start it empty, test each item, append only what passes, return after the loop.
  • The test and the kept value can differ: test status and total, keep order_id.
  • Clean text before comparing when it was typed by people.
  • Next: instead of keeping some items, build one changed value for every item.
Practise this

Loading your progress…

To master this lesson, solve both challenges without hints or the answer, and pass the concept check.

Keep the records that match — Collection Algorithms · Runtime