Modifying dictionary values while iterating with dict.values() or dict.itervalues()

Viewed 16606

I have a dicionary that is initialized like so:

tab = {'Mike': 0, 'Chad': 15, 'Taylor': 2}

I want to be able to add integers to each value in the dictionary.
For example, after adding 5, the dictionary should look like this:

{'Mike': 5, 'Chad': 20, 'Taylor': 7}

It seems as if this can be done with a couple of lines of code but I can't figure it out. I've tried a for loop:

for k in tab.itervalues():
    k = k + 5

I run this code and then print out the dictionary:

tab = {'Mike': 0, 'Chad': 15, 'Taylor': 2}

The dictionary has undergone no change. I'm not sure if Python recognizes the values as strings or integers.

2 Answers
Related