Learn Python 3: Dictionaries Cheatsheet - Codecademy
Learn Python 3: Dictionaries Cheatsheet - Codecademy
Dictionaries
Accessing and writing data in a Python dictionary
Values in a Python dictionary can be accessed
by placing the key within square brackets next to my_dictionary = {"song":
the dictionary. Values can be written by placing "Estranged", "artist": "Guns N'
key within square brackets next to the dictionary Roses"}
and using the assignment operator ( = ). If the print(my_dictionary["song"])
key already exists, the old value will be
my_dictionary["song"] = "Paradise
overwritten. Attempting to access a value with a
City"
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" .
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-dictionaries/cheatsheet Page 1 of 3
06/09/2021, 12:47
Python dictionaries
A python dictionary is an unordered collection of
items. It contains data as a set of key: value my_dictionary = {1: "L.A. Lakers",
pairs. 2: "Houston Rockets"}
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-dictionaries/cheatsheet Page 2 of 3
06/09/2021, 12:47
# with default
{"name": "Victor"}.get("nickname",
"nickname is not a key")
# returns "nickname is not a key"
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-dictionaries/cheatsheet Page 3 of 3