needmoreeasy EN 한국어
Learn

74 — Merge: joining two lists

★★★★★ (5/5)data & merging
Prerequisites
66 — Top ten, 31 — Records
You will end up with
loading two JSON lists and joining records by name key into one report

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

  1. Create two data files. students.json has one dict per student:

    [
      {"name": "Mina", "class": "A"},
      {"name": "Jun", "class": "A"},
      {"name": "Sora", "class": "B"},
      {"name": "Tom", "class": "B"}
    ]
    
    run it →
  2. scores.json has the same names with a score:

    [
      {"name": "Mina", "score": 92},
      {"name": "Jun", "score": 88},
      {"name": "Tom", "score": 75}
    ]
    
    run it →
  3. 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:

    scores_by_name = {}
    for record in scores:
        scores_by_name[record["name"]] = record["score"]
    
    run it →
  4. The full program loads both lists, joins them, and prints the combined report. Save merge.nme:

    # 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"
    
    run it →

    scores_by_name.get(name, 0) returns the score, or 0 when a student has no score yet — so Sora still appears in the report.

  5. Run it:

    nme r merge
    
    class 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.

This page on GitHub