Group Transactions by Category

Medium ⏱ 12 min 59% acceptance ★★★★☆ 4.4
Write a function total_by_category(transactions) where transactions is a list of dicts like {"category": "Food", "amount": 250}. Return a dict mapping each category to the sum of its transaction amounts.

Examples

Example 1
Input
total_by_category([{'category': 'Food', 'amount': 250}, {'category': 'Travel', 'amount': 500}, {'category': 'Food', 'amount': 100}])
Output
{'Food': 350, 'Travel': 500}
Explanation

Sum amounts per category.

Constraints

  • Every transaction has category and amount keys.

Topics

Dictionariesgroupingbusiness logic

Companies

JPMorgan ChaseDeutsche Bank

Hints

Hint 1

Use `collections.defaultdict(int)` or `.get()` accumulation, looping over transactions.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.