Phase 1: Prepare
Step 1 Warm-up recall
From M4.L3: what error does a missing key give?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- choose a list or a dictionary for a task (Foundation);
- count items with a dictionary (Core);
- read a list of dictionaries (Core).
Phase 2: Teach
Step 3 A familiar example
A survey has many answers, and you want to know how many said each thing. A tally sheet keeps one count per answer. A dictionary is the tally sheet.
Step 4 Clear explanation
Use a list when order or position matters. Use a dictionary when you look things up by name.
A dictionary makes a good counter:
words = ["a", "b", "a"]
counts = {}
for w in words:
if w in counts:
counts[w] = counts[w] + 1
else:
counts[w] = 1
print(counts){'a': 2, 'b': 1}Real data has several facts per item, so each record is a dictionary and the records sit in a list:
students = [
{"name": "Ann", "score": 70},
{"name": "Bo", "score": 85},
]
print(students[1]["name"])
total = 0
for st in students:
total = total + st["score"]
print(total)Bo
155Read students[1]["name"] from left to right: the item at position 1 (a dictionary), then its key "name". Data kept in tables and files is read this way. If a key is written in two slightly different ways, such as a gene symbol that has been changed into a date, the lookup fails or finds the wrong entry.
Step 5 Worked example (4 marks)
For data = [{"item": "pen", "qty": 3}, {"item": "ink", "qty": 2}] find data[0]["item"] and the total qty.
- data[0] is the first dictionary. (1 mark)
- Its "item" is pen. (1 mark)
- Total qty: 3 + 2 = 5. (1 mark)
- Answer: pen and 5. (1 mark)
Go deeper: the professor's notes
Items loaded.
Items reported.
Must be zero.
Check
| Count in | Count out | Action |
|---|---|---|
| 65,536 | 65,536 | investigate if input larger |
At work: Lost rows
A file has 80,000 lines.
- Count the lines.
- Count the processed items.
What a careful practitioner decides. Never report a count without comparing it with the source.
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. To look up a price by item name, use:
Lookup by name.
2. Counting how many times each word appears suits:
One count per key.
3. students[0]["name"] reads:
Position then key.
4. A list of dictionaries is good for:
Each dictionary is a record.
Phase 4: Close
Step 7 Recap
- List by position; dictionary by key.
- A dictionary counts with if key in dict.
- Records are dictionaries inside a list.
Step 8 Exit ticket (ungraded)
Count the letters in ["x", "y", "x", "x"] with a dictionary.
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Make a list of three students as dictionaries and print the highest score.
In industry: 65,536 rows
A health agency's reports in October 2020 omitted 15,841 positive cases because lab files were loaded into a format limited to 65,536 rows.
Link to this lesson. Every collection has a size. A good program checks that the count of items out equals the count of items in.
Source: GOV.UK, PHE statement on delayed reporting (4 October 2020); IT News (2020)
Common mistakes
| Mistake | Why it fails |
|---|---|
| Using a list when you need lookup | You will search every time. |
| Forgetting to add a new key before adding to it | Check with in, or set it to 1 first. |
| Mixing the order of [position][key] | Position first, then key. |
Limits. Real files often have missing or inconsistent keys; Module 6 shows how to handle them.