Key Concepts

Review core concepts you need to learn to master this subject

Accessing and writing data in a Python dictionary

my_dictionary = {"song": "Estranged", "artist": "Guns N' Roses"} print(my_dictionary["song"]) my_dictionary["song"] = "Paradise City"

Values in a Python dictionary can be accessed by placing the key within square brackets next to the dictionary. Values can be written by placing key within square brackets next to the dictionary and using the assignment operator (=). If the key already exists, the old value will be overwritten. Attempting to access a value with a key that does not exist will cause a KeyError.

To illustrate this review card, the second line of the example code block shows the way to access the value using the key "song". The third line of the code block overwrites the value that corresponds to the key "song".

Creating Dictionaries
Lesson 1 of 2