Lets say I have a dictionary with names and grades:
{"Tom" : 65, "Bob" : 90, "John" : 80...}
and I want to take all the values in the dictionary and add 10% to each:
{"Tom" : 71.5, "Bob" : 99, "John" : 88...}
How can I do it through all the values in the dictionary?
Dict comprehension:
mydict = {key:value*1.10 for key, value in mydict.items()}
Pre 2.7 :
mydict = dict(((key, value*1.10) for key, value in mydict.items()))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With