Dec-17-2017, 10:53 PM
def average(januar): k = {} # collecting the pollution values for each city # the same loop as before # I can't write it more clearly for day, data in januar.items(): for city, pollution in data.items(): if k[city]: # if the city is already in the dictionary k[city].append(pollution) # just append the pollution else: # if it is not k[city] = [] # create the key and put an empty list as a vallue. This list will holds all pollution values for this city k[city].append(pollution) # then append the pollutionThe result:
Output: k = {
'Athens': [35, 55],
'Berlin': [57, 58, 48],
'Moscow': [64, 56, 71],
'Texas': [126, 53, 34]
}
Now you can loop over it and calculate the average easy