I would create a percentage function:
def percentage(of, in_):
return in_.count(of) * 100 / len(in_)
and then apply this in the understanding of the dictionary:
>>> dct = {'Milky': ['yes', 'no', 'no', 'yes', 'no'],
... 'Chocolate': ['yes', 'no', 'yes', 'yes', 'no']}
>>>
>>> {key: percentage('yes', values) for key, values in dct.items()}
{'Chocolate': 60.0, 'Milky': 40.0}
source
share