Add Key-Value Pairs (Solution)
00:00
To add a key-value pair to your dictionary, use the square bracket notation. If you want to add Enterprise
as a key and Picard
as the value, you write captains
and then an opening square bracket, and then inside of quotes, the name of the ship, which is Enterprise
in this case.
00:23
Then a quote to close the string, and the closing square bracket equals the string Picard
. Once you press enter, you can check the captains
dictionary that you created in one of the former lessons.
00:40
It still is a dictionary, but now it contains the key-value pair Enterprise
and Picard
. Let’s do this for the other ships and captains.
00:52
So you write captains
and then square brackets. Voyager
equals Janeway
00:59
and captains
. Square brackets Defiant
equals Sisko
.
01:07
And if you check the captains
dictionary now, you can see that the dictionary now has three key-value pairs: Enterprise
, Picard
, Voyager
, Janeway
, and Defiant
, Sisko
, which you entered one item at a time.
Become a Member to join the conversation.