Can we overwrite a key?

Can we overwrite a key? In this exercise we overwrote a value, but what if I had a dictionary {3: “apple”}, can i change the key ‘3’?

Short answer: no.

Longer: what do you want to do?

  1. Have a new entry, alongside the old: d = {3: 'apple', 4: 'apple'}
  2. Have a new entry, removing the old: d = {4: 'apple'}
  3. Something else?

You can do #1 by just adding a new entry
You can do #2 by adding a new entry and del d[3]
There are existing methods to do #3, whatever it is.


Yes, you can do it using the pop() method

mydict["new_key"] = mydict.pop("old_key")


From this suggestion:
Yes, you can do it using the pop() method

mydict["new_key"] = mydict.pop("old_key")

I tried adding this code to the prior exercise and I got an error. i tried changing the key “Best Picture” to “Worst Picture”

oscar_winners = {“Best Picture”: “La La Land”, “Best Actor”: “Casey Affleck”, “Best Actress”: “Emma Stone”, “Animated Feature”: “Zootopia”}
oscar_winners.update({“Supporting Actress”: “Viola Davis”})
oscar_winners[“Best Picture”] = “Moonlight”
print(oscar_winners[“Best Picture”])
#Using .pop to change a key. See below
oscar_winners[“Best Picture”] = oscar_winners.pop(“Worst Picture”)
print("After pop ", oscar_winners)

In jupyter notebook I got this error:

KeyError Traceback (most recent call last)
5 print(oscar_winners)
6 #Using .pop to change a key. See below
----> 7 oscar_winners[“Best Picture”] = oscar_winners.pop(“Worst Picture”)
8 print("After pop ", oscar_winners)

KeyError: ‘Worst Picture’

Can you tell me what I did wrong employing the .pop?

Please see the following as formatted code is much easier for eveyone to read- How do I format code in my posts?

The error points you to the issue, a KeyError suggests that key doesn’t exist for the dictionary.

If you look at the docs for dict.pop- Built-in Types — Python 3.9.2 documentation you’ll note that it searches for an existing key, removes that key from the dict and returns the value. Is “worst picture” an existing key? Double check what goes where.

I just saw this and thought I would put my 3.67 cents in. In your code, you are popping a key out that doesn’t exist yet. Popping removes the dictionary key, it doesn’t add another one in. In your original oscar_winners dictionary, you don’t have a “Worst Picture.” So, when you try to remove “Worst Picture,” Python can’t do it because it doesn’t exist.

A recommendation could be:

oscar_winners = {"Best Picture": "La La Land", "Best Actor": "Casey Affleck", "Best Actress": "Emma Stone", "Animated Feature": "Zootopia"}
oscar_winners["Supporting Actress"] = "Viola Davis"
oscar_winners["Best Picture"] = "Moonlight"
#issue solved here
oscar_winners["Worst Picture"] = oscar_winners["Best Picture"]
oscar_winners.pop("Best Picture")

print("After pop ", oscar_winners)

In other words, you can pop out best picture and the value associated with it. You just have to create a new key and assign it the same value as best picture before you pop best picture out.