← QCE Digital Solutions Unit 2

Filtering a stream of records

Keep only the records in a stream that meet a condition and discard the rest, usually with an if inside a loop that adds matches to a result list.

Understanding Filtering a stream of records in QCE Digital Solutions Unit 2

What it does: The empty list collects matches, the for loop visits each record, and the if inside the loop decides which records are appended. Filtering selects records. It does not change them. Records that fail the condition are left out of the result. and needs BOTH tests to be true. or would also keep Year 10 students with high points and Year 11 students with low points. A single = assigns a value. A condition needs the comparison operator == to test equality. The and operator joins two tests, so a record must satisfy both to pass the filter.

Common mistakes: The condition is reversed. <= keeps students with 50 or less, so the passed list would hold the failures. It should be >= 50 so that only students who passed are kept. A for loop header must end with a colon. It should be for p in people: so the indented filter code forms the loop body.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 186–228) and word search (Modules 144–228).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which best describes filtering a stream of records?
Change every record so it has a new value in one fieldKeep only the records that meet a condition and discard the rest
Matches
for row in rows: if row[2] > 80: result = [] result.append(row)
↓
The result list is created inside the if, so it is reset to empty for each match and only the last match survives. It must be created once before the loop.
Applying
Meanings
Term
filter
Meaning
Keep only the records in a stream that meet a condition and discard the rest, usually with an if inside a loop that adds matches to a result list.
Practice
records = [("Ava", 88), ("Liam", 65), ("Mia", 95), ("Noah", 52)]
Testing
Gaps
p["age"] >= 18: adults.append(p)
Check Answer
Spots
2for s in students:
3 if s['mark'] <= 50:

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Sand Cat' badge — Precision Prowler, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which best describes filtering a stream of records?
A. Change every record so it has a new value in one field
B. Keep only the records that meet a condition and discard the rest ✓
Filtering selects records. It does not change them. Records that fail the condition are left out of the result.
2. Which code correctly filters students with a mark of 50 or more into a result list?
A. for s in students: if s["mark"] >= 50: passed.append(s) ✓
B. for s in students: passed.append(s) if s["mark"] >= 50:
The if sits inside the loop and guards the append. Only records that pass the test are added to the result list.
3. Where should the empty result list be created when filtering?
A. Inside the loop on every pass
B. Before the loop starts ✓
If the list is created inside the loop, it is reset on every pass and only the last match would survive. Create it once, before the loop.
4. A stream has 10 records and 4 meet the condition. How many records are in the filtered result?
A. 4 ✓
B. 6
The result holds the records that meet the condition. The 6 that fail are discarded.
5. Which compound condition keeps students in Year 11 who also have more than 100 points?
A. if s["year"] == 11 and s["points"] > 100 ✓
B. if s["year"] == 11 or s["points"] > 100
and needs BOTH tests to be true. or would also keep Year 10 students with high points and Year 11 students with low points.

Match the Pairs

if mark >= 50: passed.append(row) → Keeps the record only when its mark meets the condition, by adding it to the passed list
if age >= 16 and house == 'Red': → A compound condition: a record is kept only if both tests are true
Build a result list by starting with an empty list, looping over every record, and adding only the matches → result = [] for row in rows: if row[2] > 80: result.append(row)
A filter that keeps students who are in Year 11 or Year 12 using a compound condition → if year == 11 or year == 12:
for row in rows: if row[2] > 80: result = [] result.append(row) → The result list is created inside the if, so it is reset to empty for each match and only the last match survives. It must be created once before the loop.
for row in rows: result.append(row) if row[2] > 80: print(row) → The append is outside the if, so every record is added and nothing is filtered out. The append must be inside the if so only matches are kept.

Key Term

filter
Keep only the records in a stream that meet a condition and discard the rest, usually with an if inside a loop that adds matches to a result list.

Fill in the Blanks

1. adults = [] for p in people: if p["age"] >= 18: adults.append(p)
Hint: Type the keyword that tests a condition inside the loop to decide whether to keep each record.
2. high = [] for r in records: if r["mark"] >= 80: high.append(r)
Hint: Type the comparison operator that means greater than or equal to.
3. for s in rows: if s["yr"] == 11 and s["m"] > 70: out.append(s)
Hint: Type the Boolean operator that requires both conditions to be true before a record is kept.
4. matches = [] for t in tasks: if t["done"]: matches.append(t)
Hint: Type the name of the result list variable that the loop adds matching records to.
5. cheap = [] for p in items: if p["price"] < 10: cheap.append(p)
Hint: Type the list method that adds a matching record to the end of the result list.

Spot the Error

Question 1
A passed = []
B for s in students:
C if s['mark'] <= 50:
D passed.append(s)
Line C — The condition is reversed. <= keeps students with 50 or less, so the passed list would hold the failures. It should be >= 50 so that only students who passed are kept.
Question 2
A adults = []
B for p in people
C if p['age'] >= 18:
D adults.append(p)
Line B — A for loop header must end with a colon. It should be for p in people: so the indented filter code forms the loop body.
Question 3
A result = []
B for r in records:
C if r['house'] = 'Red':
D result.append(r)
Line C — A single = assigns a value, and it is not allowed in an if condition. Comparing for equality needs ==, so it should be if r['house'] == 'Red':.
Question 4
A keep = []
B for n, yr, pts in keep:
C if yr == 7 and pts > 90:
D keep.append(n)
Line B — The loop must read through the incoming stream of records (rows). It loops over keep, the empty result list, so nothing is ever checked. It should be for n, yr, pts in rows:.
Question 5
A matches = 0
B for r in records:
C if r['score'] > 80:
D matches.append(r)
Line A — The result list must start as an empty list, []. With matches = 0 it is a number, so matches.append(r) fails. It should be matches = [].

Student with a class code? Join your class →