You have a list of transactions and you want a dict like {"Food": 20.0, "Coffee": 4.5} — one total per category. How do you build a dict one update at a time?
Start with an empty dict, loop through, add each amount to the right key?
Exactly the accumulator shape — but on a dict. The wrinkle: the first time you see a category, the key doesn't exist yet. Direct access would raise KeyError. That's where .get(key, 0.0) shines:
totals[cat] = totals.get(cat, 0.0) + t["amount"]Reads the current total for cat (or 0.0 if missing), adds the new amount, stores it back.
So .get() does double duty — it handles both the first-time case and the existing-total case in one line?
Yes. One expression initializes and updates. No if key in totals: branching needed.
What if the transactions list is empty?
The for loop doesn't run, and you return the empty dict you started with. No special case:
def category_totals(txns):
totals = {}
for t in txns:
cat = t["category"]
totals[cat] = totals.get(cat, 0.0) + t["amount"]
return totalsSo a dict accumulator looks exactly like a scalar accumulator — initialize, loop-and-update, return. Just with .get() guarding the missing-key case.
Same three beats, same skeleton. The dict version is a direct generalization — one slot per key instead of one single running total. You'll see this shape in every aggregation problem from now on.
TL;DR: totals.get(key, 0.0) + value — accumulate without an if branch.
totals = {}for t in txns:totals[cat] = totals.get(cat, 0.0) + t["amount"]| State | totals.get(cat, 0.0) |
|---|---|
cat not in dict | 0.0 (default) |
cat already exists | current total |
One expression handles both cases. No if cat in totals: guard needed.
Write `category_totals(txns)` that returns a dict mapping each category to its total amount. Example: `[{"category": "Food", "amount": 12.0}, {"category": "Food", "amount": 8.0}]` returns `{"Food": 20.0}`.
Tap each step for scaffolded hints.
No blank-editor panic.
You have a list of transactions and you want a dict like {"Food": 20.0, "Coffee": 4.5} — one total per category. How do you build a dict one update at a time?
Start with an empty dict, loop through, add each amount to the right key?
Exactly the accumulator shape — but on a dict. The wrinkle: the first time you see a category, the key doesn't exist yet. Direct access would raise KeyError. That's where .get(key, 0.0) shines:
totals[cat] = totals.get(cat, 0.0) + t["amount"]Reads the current total for cat (or 0.0 if missing), adds the new amount, stores it back.
So .get() does double duty — it handles both the first-time case and the existing-total case in one line?
Yes. One expression initializes and updates. No if key in totals: branching needed.
What if the transactions list is empty?
The for loop doesn't run, and you return the empty dict you started with. No special case:
def category_totals(txns):
totals = {}
for t in txns:
cat = t["category"]
totals[cat] = totals.get(cat, 0.0) + t["amount"]
return totalsSo a dict accumulator looks exactly like a scalar accumulator — initialize, loop-and-update, return. Just with .get() guarding the missing-key case.
Same three beats, same skeleton. The dict version is a direct generalization — one slot per key instead of one single running total. You'll see this shape in every aggregation problem from now on.
TL;DR: totals.get(key, 0.0) + value — accumulate without an if branch.
totals = {}for t in txns:totals[cat] = totals.get(cat, 0.0) + t["amount"]| State | totals.get(cat, 0.0) |
|---|---|
cat not in dict | 0.0 (default) |
cat already exists | current total |
One expression handles both cases. No if cat in totals: guard needed.