Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

How to Modify Values in a Dictionary While Iterating Through It

Kiran on Dec. 29, 2019

Modify keys in the dictionary is nothing but adding new keys but not updating any existing key. is that right?

Liam Pulsifer RP Team on Dec. 30, 2019

@Kiran sure, I suppose that’s reasonable. I used “modify keys” to refer to adding or deleting keys because Python doesn’t offer an explicit operation (that I know of) to modify keys in a dictionary, other than deleting one and adding another.

mikesult on May 5, 2020

It make sense that you can’t “modify” a key since it is immutable. So just delete the old and add a new one if you needed to change the key.

del prices['apple']
prices['granny_smith_apple'] = .5

Also it follows that trying to update ‘value’ doesn’t work because key and value are part of an immutable tuple. I’m kind of surprised it doesn’t throw an error.

for key, value in prices.items():
    value = value + 0.2
# no change in prices

Liam Pulsifer RP Team on May 6, 2020

@mikesult these are great intuitions you’re developing, especially regarding the dictionary keys. I want to quickly clarify something about your comment on value updating – the reason that trying to update value in the way you demonstrate does nothing productive but also doesn’t throw an error is because under the hood, it looks like this

for tuple in prices.items():
    key, value = tuple
    value = value + 0.2

So in reality, you’re not trying to modify the tuple. What you’re really modifying is simply a new variable value that has the same value as the corresponding element of the tuple. Of course, that has nothing to do with the real “value” that resides in the dictionary. If, on the other hand, you tried to do this

for tuple in prices.items():
    tuple[1] = tuple[1] + 0.2

you would get an error for trying to assign to a tuple. I hope these subtleties make sense!

mikesult on May 6, 2020

Perfect explanation. Thanks.

yasserkgl on May 25, 2020

hello I have the below error message

prices={'orange':'4', 'apple': 3, 'banana':7}
for key in prices:
    prices[key] = prices[key] + 1
    print(prices)

output

Traceback (most recent call last):
  File "D:/Users/y.moneim/PycharmProjects/untitled2/Test002.py", line 62, in <module>
    prices[key] = prices[key] + 1
TypeError: can only concatenate str (not "int") to str

jaosunita on June 5, 2020

@yasserkgl, apostrophes in ‘4’ must be eliminated. Regards!

Liam Pulsifer RP Team on June 5, 2020

Good question @yasserkgl and good answer @jaosunita!

Become a Member to join the conversation.