Delete a Dictionary Key (Solution)
00:00
Since we apparently don’t know who the captain of Discovery is, we need to delete it. You do this by using the del
keyword and then the dictionary’s name, and then most importantly, again, in square brackets, the key name, which is Discovery
in this case with a closing square bracket.
00:24
Now, don’t forget the square bracket notation because if you would just write del captains
, you would delete the whole dictionary from the namespace.
00:34 And with the square bracket notation of the key, you make sure you only want to delete this item of the dictionary and not the whole dictionary. So that’s very important.
00:44
And once you press enter and Python performs the action, you can verify that the dictionary still contains the other items by writing captains
.
00:53
And there you can see we are back again to the dictionary state with the keys Enterprise
, Voyager
and Defiant
, and the values Picard
, Janeway
and Sisko
.
01:05 And I have a feeling that this was enough exercising and that there is a challenge on the horizon. Let’s see what I see next.
Become a Member to join the conversation.