CS101 Dictionaries
Learn how to create and use dictionaries in Python.
StartKey Concepts
Review core concepts you need to learn to master this subject
Accessing and writing data in a Python dictionary
Syntax of the Python dictionary
Merging Dictionaries with the .update()
Method in Python
Dictionary value types
Python dictionaries
Dictionary accession methods
get() Method for Dictionary
The .pop()
Method for Dictionaries in Python
Accessing and writing data in a Python dictionary
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"
.