I am trying to find the averages for the values of a dictionary by city. For the purposes of this exercise I cannot use numpy or pandas.
Here is some example data:
d = {
('Chicago', 2006): 23.4,
('Chicago', 2007): 73.4,
('Dallas', 2008): 70.8,
('Paris', 2010): 5.6,
('Paris', 2011): 63.3)
}
Here is the ideal output:
city_averages = {
'Chicago': 48.4,
'Dallas': 70.8,
'Paris': 139.7
}
Here is the code I tried.
city_averages = {}
total = 0
for k,v in d.items():
total += float(v)
city_averages[k[0]] = total