Python Code Challenges: Dictionaries

Link to the specific code challenge: https://www.codecademy.com/courses/learn-python-3/articles/python-code-challenges-dictionaries

I’m confused why the following code adds to the values and not the keys. I just cannot seem to wrap my head around using .keys() adds to values?:

The code is working as expected, as instructed. The values are expected to change. As for changing the value of the key, that cannot be done. Keys are immutable. The only thing we can do with them is delete. Delete the key and the associated value disappears along with it.

1 Like

Thanks for the response! I forgot that keys are immutable, so that’s a helpful reminder. I guess what I’m still confused about is the “why” behind why when using .keys() it affects the values?

1 Like

That is because you are accessing the value by its key.

dict[key] => value
2 Likes

Aah, that is a helpful way to think about it. Thank you!

1 Like