74 — Merge: joining two lists
Real data often lives in more than one file. A school keeps students in one list and their scores in another. Joining them means looking a name up in the second list — exactly what a dict is for.
Steps
Create two data files.
students.jsonhas one dict per student:
run it →[ {"name": "Mina", "class": "A"}, {"name": "Jun", "class": "A"}, {"name": "Sora", "class": "B"}, {"name": "Tom", "class": "B"} ]scores.jsonhas the same names with a score:
run it →[ {"name": "Mina", "score": 92}, {"name": "Jun", "score": 88}, {"name": "Tom", "score": 75} ]Build a lookup dict from the scores: each name maps to its score, so finding a student's score is one fast
[]lookup instead of a scan:
run it →scores_by_name = {} for record in scores: scores_by_name[record["name"]] = record["score"]The full program loads both lists, joins them, and prints the combined report. Save
merge.nme:
run it →# merge.nme — join students with scores by name. # Run: nme r merge # students.json and scores.json must be in the same folder. use file latest students = json_load("students.json") scores = json_load("scores.json") scores_by_name = {} for record in scores: scores_by_name[record["name"]] = record["score"] show f"class report ({len(students)} students):" for student in students: name = student["name"] score = scores_by_name.get(name, 0) show f" {name} in class {student['class']}: {score} points"scores_by_name.get(name, 0)returns the score, or0when a student has no score yet — so Sora still appears in the report.Run it:
nme r mergeclass report (4 students): Mina in class A: 92 points Jun in class A: 88 points Sora in class B: 0 points Tom in class B: 75 points
Try it yourself
Add a second score for Mina in scores.json and change the lookup to keep
the highest score, or add a grade field derived from the score in the loop.
What you learned
- A dict turns a name into a value for fast lookup.
- Joining two lists means building a lookup dict from one and looping the other.
dict.get(key, default)handles missing keys without crashing.- Merging is how programs combine data that lives in separate files.