Filtering a stream of records

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Filtering a stream of records

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
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:
3. Where should the empty result list be created when filtering?
A Inside the loop on every pass
B Before the loop starts
4. A stream has 10 records and 4 meet the condition. How many records are in the filtered result?
A 4
B 6
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1if mark >= 50: passed.append(row) AThe 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.
2if age >= 16 and house == 'Red': BA compound condition: a record is kept only if both tests are true
3Build a result list by starting with an empty list, looping over every record, and adding only the matches Cif year == 11 or year == 12:
4A filter that keeps students who are in Year 11 or Year 12 using a compound condition DThe 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.
5for row in rows: if row[2] > 80: result = [] result.append(row) Eresult = [] for row in rows: if row[2] > 80: result.append(row)
6for row in rows: result.append(row) if row[2] > 80: print(row) FKeeps the record only when its mark meets the condition, by adding it to the passed list

Definitions

filter

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
adults = [] for p in people: __ p["age"] >= 18: adults.append(p)
2.
high = [] for r in records: if r["mark"] __ 80: high.append(r)
3.
for s in rows: if s["yr"] == 11 ___ s["m"] > 70: out.append(s)
4.
_______ = [] for t in tasks: if t["done"]: matches.append(t)
5.
cheap = [] for p in items: if p["price"] < 10: cheap.______(p)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A passed = []
B for s in students:
C if s['mark'] <= 50:
D passed.append(s)
Question 2
A adults = []
B for p in people
C if p['age'] >= 18:
D adults.append(p)
Question 3
A result = []
B for r in records:
C if r['house'] = 'Red':
D result.append(r)
Question 4
A keep = []
B for n, yr, pts in keep:
C if yr == 7 and pts > 90:
D keep.append(n)
Question 5
A matches = 0
B for r in records:
C if r['score'] > 80:
D matches.append(r)

Answer Key

Filtering a stream of records
Section A: Quick Questions
Q1
B — Keep only the records that meet a condition and discard the rest
Q2
A — for s in students: if s["mark"] >= 50: passed.append(s)
Q3
B — Before the loop starts
Q4
A — 4
Q5
A — if s["year"] == 11 and s["points"] > 100
Section B: Match the Pairs
Q1
if mark >= 50: passed.append(row) → Keeps the record only when its mark meets the condition, by adding it to the passed list
Q2
if age >= 16 and house == 'Red': → A compound condition: a record is kept only if both tests are true
Q3
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)
Q4
A filter that keeps students who are in Year 11 or Year 12 using a compound condition → if year == 11 or year == 12:
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
adults = [] for p in people: if p["age"] >= 18: adults.append(p)
Q2
high = [] for r in records: if r["mark"] >= 80: high.append(r)
Q3
for s in rows: if s["yr"] == 11 and s["m"] > 70: out.append(s)
Q4
matches = [] for t in tasks: if t["done"]: matches.append(t)
Q5
cheap = [] for p in items: if p["price"] < 10: cheap.append(p)
Section E: Spot the Error
Q1
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.
Q2
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.
Q3
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':.
Q4
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:.
Q5
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 = [].