I am trying to make a dictionary using the output of a function I wrote (dct) that parses an income and spending .csv file. I designated "amount" as the total of the transaction. dct creates a dictionary that returns this:
spending = {
'month': [],
'payee': [],
'amount': [],
'category': [],
}
The goal of this next function is to make a dictionary that sums all the "amount" outputs per "category" (only car, home, and food). This is my code so far, and it works. Still, I believe is not the most pythonic way to write it. Any ideas?
def summarize_by_category(dct):
car, home, food = 0, 0, 0
for (index,category) in enumerate( dct['category'] ):
if "car" in category:
car += dct['amount'][index]
elif "food" in category:
food += dct['amount'][index]
elif "home" in category:
home += dct['amount'][index]
Totals = {
'car': car,
'food': food,
'home': home,
}
return (Totals)
in categoryinstead of== category? Can the category becars,racecar, etc.?[{"category": "car", "amount": 10, "payee": "gas station", "month": "June"}, ...]