Loading…
Use dictionaries to group data by category.
sales = [
{"category": "Food", "amount": 100},
{"category": "Tech", "amount": 500},
{"category": "Food", "amount": 150}
]totals = {}
for sale in sales:
cat = sale["category"]
if cat not in totals:
totals[cat] = 0
totals[cat] += sale["amount"]
Given the data below, calculate and print total sales per region:
orders = [
{"region": "East", "sales": 200},
{"region": "West", "sales": 350},
{"region": "East", "sales": 450},
{"region": "West", "sales": 200}
]
Downloading Python… (one-time, ~10 MB)
This runs entirely in your browser and is cached for next time.