Quiz 2
Registry Synced

python-week8

553 words
3 min read

Reading compass

Now · Week map

Deep study for Quiz 2 week 8. Real programs combine lists, tuples, dicts, and strings — know which structure fits each sub-problem.

Week map

Choose structure → list of dicts → dict of lists → nested access → aggregate patterns → common mixed traces.

Mixed structure notation

  • List of dicts → table rows → [{"name": "A", "score": 90}, ...].
  • Dict of lists → columns keyed by field → {"names": [...], "scores": [...]}.
  • Dict of dicts → nested lookup → data[user][metric].
  • Tuple in dict → immutable key bundle → {(x, y): value} for grid cells.
Mini-example:
python
students = [
    {"id": 1, "grade": 85},
    {"id": 2, "grade": 91},
]
students[1]["grade"]   # 91

Access chain

Read inside-out: container[index_or_key][next_key]...
python
matrix = [1, 2], [3, 4](/viewer?path=1, 2], [3, 4)
matrix[1][0]           # 3

registry = {"team": {"lead": "Sam", "size": 5}}
registry["team"]["lead"]  # 'Sam'

Common mixed patterns

Filter and collect

python
results = []
for row in table:
    if row["score"] >= 60:
        results.append(row["name"])

Group by key

python
groups = {}
for item in items:
    k = item["category"]
    if k not in groups:
        groups[k] = []
    groups[k].append(item)

Sort list of dicts (concept)

Sort by field: sorted(rows, key=lambda r: r["score"]). Know that key picks comparison field.

Pattern families

Easy — Nested access

One or two-level lookup. Predict type after access: list element, dict value, character in string.

Medium — Build mixed structure

Construct list of dicts from parallel lists. Update nested value. Count items matching condition across records.

Hard — Aggregate over mixed data

Average scores per category. Find record with max field. Merge two list-of-dict sources by shared key.

Worked mini-examples

Example 1 — List of dicts.
python
books = [{"title": "A", "pages": 200}, {"title": "B", "pages": 150}]
total = 0
for b in books:
    total += b["pages"]
# total = 350
Example 2 — Dict of lists.
python
data = {"x": [1, 2], "y": [3, 4]}
data["x"].append(5)   # {"x": [1, 2, 5], "y": [3, 4]}
Example 3 — Filter names.
python
rows = [{"n": "a", "ok": True}, {"n": "b", "ok": False}]
passed = [r["n"] for r in rows if r["ok"]]
# ['a']
Example 4 — Tuple key.
python
grid = {}
grid[(0, 0)] = "start"
grid[(1, 0)] = "path"
grid[(0, 0)]   # 'start'
Example 5 — Type discipline.
python
record = {"tags": ["py", "stats"]}
record["tags"].append("ml")   # list inside dict — mutable
record["tags"][0]             # 'py'

Traps

  • Wrong bracket type: d["key"] vs lst[0].
  • Shallow copy of list-of-lists shares inner lists.
  • Iterating dict gives keys, not values — use .values() or .items().
  • Assuming all rows have same keys — missing key raises KeyError.
  • Modifying list while iterating — use new list or iterate copy.

Diagnostic (try yourself)

  1. Given d = {"a": [1, 2], "b": [3]}, what is d["a"][1]?
  2. Build a list of dicts with keys "name" and "age" from names = ["Kim", "Lee"] and ages = [20, 22].
  3. From records = [{"v": 3}, {"v": 7}, {"v": 5}], how would you find the dict with largest "v"?
  4. What is data[0]["x"] if data = [{"x": 10}, {"x": 20}]?
  5. Why might groups[key].append(item) require checking if key not in groups first?
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.