You have the list of transactions with category and amount fields, and you want {"Food": 25.0, "Gas": 40.0} — one number per category, summing all amounts in that category. What's the pattern?
Accumulator. A dict that starts empty, and for each transaction, add its amount to the running total for that category.
Exactly. The accumulator pattern with .get(key, 0) as the default — if the category is new, the current total is zero, then we add the new amount. One line inside the loop:
totals[item["category"]] = totals.get(item["category"], 0) + item["amount"]Read it: "current total (or 0 if new) plus this item's amount, stored back under the category key."
Can setdefault() also work here, or is .get() cleaner for this case?
.get() is cleaner for numeric accumulation. setdefault is best when you want to modify the existing value in-place (like .append() on a list). For sums, .get(key, 0) + new_value then assign is the idiom:
def aggregate_totals(items: list) -> dict:
totals = {}
for item in items:
cat = item["category"]
totals[cat] = totals.get(cat, 0) + item["amount"]
return totalsInitialize empty, iterate, accumulate, return.
So for an empty input list, totals stays {}?
Right. Loop never runs, return the empty dict. Same graceful behavior as grouping. And if every item is in the same category, you get a one-key dict with the sum of all amounts.
This works for counts too, right? Change + item["amount"] to + 1 and you have a frequency counter per category.
Exactly — the accumulator pattern is shape-agnostic. Sum, count, concatenate, max-of — any operation that reduces many values to one, grouped by a key. You're building the backbone of SQL's SELECT category, SUM(amount) GROUP BY category in pure Python.
TL;DR: totals[key] = totals.get(key, 0) + value — accumulate per key in one line.
.get(key, 0) — safe lookup with zero default+ value to + 1 for counts, max(x, current) for max, etc.{}, returns {} on empty input| Goal | Inner-loop expression |
|---|---|
| Sum by key | d[k] = d.get(k, 0) + v |
| Count by key | d[k] = d.get(k, 0) + 1 |
| Max by key | d[k] = max(d.get(k, v), v) |
Same accumulator shape — only the reduction operation changes.
You have the list of transactions with category and amount fields, and you want {"Food": 25.0, "Gas": 40.0} — one number per category, summing all amounts in that category. What's the pattern?
Accumulator. A dict that starts empty, and for each transaction, add its amount to the running total for that category.
Exactly. The accumulator pattern with .get(key, 0) as the default — if the category is new, the current total is zero, then we add the new amount. One line inside the loop:
totals[item["category"]] = totals.get(item["category"], 0) + item["amount"]Read it: "current total (or 0 if new) plus this item's amount, stored back under the category key."
Can setdefault() also work here, or is .get() cleaner for this case?
.get() is cleaner for numeric accumulation. setdefault is best when you want to modify the existing value in-place (like .append() on a list). For sums, .get(key, 0) + new_value then assign is the idiom:
def aggregate_totals(items: list) -> dict:
totals = {}
for item in items:
cat = item["category"]
totals[cat] = totals.get(cat, 0) + item["amount"]
return totalsInitialize empty, iterate, accumulate, return.
So for an empty input list, totals stays {}?
Right. Loop never runs, return the empty dict. Same graceful behavior as grouping. And if every item is in the same category, you get a one-key dict with the sum of all amounts.
This works for counts too, right? Change + item["amount"] to + 1 and you have a frequency counter per category.
Exactly — the accumulator pattern is shape-agnostic. Sum, count, concatenate, max-of — any operation that reduces many values to one, grouped by a key. You're building the backbone of SQL's SELECT category, SUM(amount) GROUP BY category in pure Python.
TL;DR: totals[key] = totals.get(key, 0) + value — accumulate per key in one line.
.get(key, 0) — safe lookup with zero default+ value to + 1 for counts, max(x, current) for max, etc.{}, returns {} on empty input| Goal | Inner-loop expression |
|---|---|
| Sum by key | d[k] = d.get(k, 0) + v |
| Count by key | d[k] = d.get(k, 0) + 1 |
| Max by key | d[k] = max(d.get(k, v), v) |
Same accumulator shape — only the reduction operation changes.
Write `aggregate_totals(items)` that takes a list of dicts (each with `"category"` and `"amount"` keys) and returns a dict mapping each category to the sum of amounts in that category.
Tap each step for scaffolded hints.
No blank-editor panic.