Dictionaries and records: give your data fields names
Represent observations with named fields, distinguish missing from zero, and build a grouped summary.
Learning lesson · Reviewed October 2, 2026. Allow additional time for the practice. Prerequisites: lists, conditions, and functions. Python 3; no extra packages.
- Read and update named fields in a dictionary.
- Choose explicitly how to handle a missing key.
- Aggregate records while keeping the observation unit clear.
Position-based data is easy to misread. In ["A01", "Python", 80], you need to remember what each position means. A record such as {"learner": "A01", "course": "Python", "score": 80} makes the fields visible. A list can hold many such records. All examples in this lesson are fictional; identifiers refer to invented learners.
Keys describe fields; values hold observations
record = {"learner": "A01", "course": "Python", "score": 80}
print(record["course"]) # Python
record["score"] = 82
print(record["score"]) # 82
print(record.get("comment")) # NoneBrackets access a required key and raise KeyError if it is absent. get() can return a default for a missing key. That default is a decision, not proof that the missing value was actually observed. For required fields, letting a missing-key error reveal a broken record may be more useful than quietly inventing a replacement.
| Record situation | Meaning in this exercise | Appropriate action |
|---|---|---|
| score is 0 | An observed score of zero | Keep the zero |
| score is None | Explicitly missing observation | Exclude from mean; count as missing |
| score key absent | Record does not match expected schema | Investigate or reject |
| score is "80" | Text requiring conversion | Validate before numeric analysis |
Define the unit of observation
Here one record is one submitted assessment. A learner may submit more than one assessment. Counting records therefore counts submissions, not necessarily unique people. This distinction becomes important when someone labels a grouped total “learners.” Decide what one row represents before interpreting any summary.
records = [
{"learner": "A01", "course": "Python", "score": 80},
{"learner": "A02", "course": "Python", "score": 0},
{"learner": "A03", "course": "Data", "score": 70},
{"learner": "A04", "course": "Data", "score": None},
]
groups = {}
missing = 0
for record in records:
score = record["score"]
if score is None:
missing += 1
continue
course = record["course"]
if course not in groups:
groups[course] = {"total": 0, "count": 0}
groups[course]["total"] += score
groups[course]["count"] += 1
for course, stats in groups.items():
print(course, stats["count"], stats["total"] / stats["count"])
print("Missing:", missing)
# Python 2 40.0
# Data 1 70.0
# Missing: 1The continue moves to the next record after recording a missing score. Zero remains in the Python group, so its mean is (80 + 0) / 2 = 40. Replacing score is None with if not score would incorrectly skip zero as well. A short condition can hide a large analytical choice.
Named records→Check
Missing score?→Accumulate
Total + count→Report
Mean per group
Inspect what the summary leaves out
The Data group has one valid score and one missing score. Its mean of 70 says nothing about the missing submission. The Python group has two valid scores with a wide difference. Neither number proves that one course is better: these invented samples are tiny, and the assessments may not be comparable. A useful report includes counts and limitations alongside averages.
Practice: count submissions by course
Build a dictionary of submission counts using all four records, including the missing score. Expected result: Python has two submissions and Data has two. Explain why this count differs from the number of valid scores used in each mean.
Show the worked solution
counts = {}
for record in records:
course = record["course"]
counts[course] = counts.get(course, 0) + 1
print(counts) # {'Python': 2, 'Data': 2}The default zero initializes a count of events, not an unknown score. Every record contributes to submissions, while only observed numeric scores contribute to the mean.
Before moving on
Be able to say what one record represents, which keys are required, and how zero differs from missing. Next, you will validate text before converting it so that malformed data does not silently enter this grouped calculation.
Official sources checked October 2, 2026. Examples, exercises, and rubrics are original Noetrion teaching material; fictional datasets are identified below.