How can you average the value in a dictionary if it's a list? What follows is the code I have with the correct answer. I cannot use slicing techniques
dict = {'zy': [1.0, 0.9, 0.8, 0.7]}
# code to average the values
print(dict)
{'zy': 0.85}
How can you average the value in a dictionary if it's a list? What follows is the code I have with the correct answer. I cannot use slicing techniques
dict = {'zy': [1.0, 0.9, 0.8, 0.7]}
# code to average the values
print(dict)
{'zy': 0.85}
Just use simple maths:
dict['zy'] = sum(dict['zy']) / len(dict['zy'])
If you don't want to implement yourself you can use the statistics.mean
function:
from statistics import mean
...
dict['zy'] = mean(dict['zy'])
As a side note, don't use built-ins such as dict
to name your variables. You will shadow the built-in.