I have a dictionary. I want to calculate average of values for each key and print result so that the result shows key and associated average. The following code calculates mean
Don't use a list comprehension. Use a dictionary comprehension to calculate the average of each list. You can also from __future__ import division to avoid having to use float:
>>> from __future__ import division
>>> d = {22: [1, 0, 0, 1], 23: [0, 1, 2, 1, 0], 24: [3, 3, 2, 1, 0]}
>>> mean = {k: sum(v) / len(v) for k, v in d.iteritems()}
>>> mean
{22: 0.5, 23: 0.8, 24: 1.8}