2.0 Python: Access Dictionary Items
2.1 Accessing Items
You can access the items of a dictionary by referring to its key name, inside square brackets:
2.1.1 Example
Get the value of the "model" key:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict["model"])
There is also a method called get()
that will give you the same result:
2.1.2 Example
Get the value of the "model" key:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict.get("model"))
2.2 Get Keys
The keys()
method will return a list of all the keys in the dictionary.
2.2.1 Example
Get a list of the keys:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict.keys())
The list of the keys is a view of the dictionary, meaning that any changes done to the dictionary will be reflected in the keys list.
2.2.2 Example
Add a new item to the original dictionary, and see that the keys list gets updated as well:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(car.keys())
car["color"] = "white"
print(car.keys())
2.3 Get Values
The values()
method will return a list of all the values in the dictionary.
2.3.1 Example
Get a list of the values:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car["color"] = "white"
print(car.values())
The list of the values is a view of the dictionary, meaning that any changes done to the dictionary will be reflected in the values list.
2.3.2 Example
Make a change in the original dictionary, and see that the values list gets updated as well:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(car.values())
car["year"] = 2020
print(car.values())
2.3.3 Example
Add a new item to the original dictionary, and see that the values list gets updated as well:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.values()
print(car.values())
car["color"] = "red"
print(x)
2.4 Get Items
The items()
method will return each item in a dictionary, as tuples in a list.
2.4.1 Example
Get a list of the key:value
pairs
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(car.items())
The returned list is a view of the items of the dictionary, meaning that any changes done to the dictionary will be reflected in the items list.
2.4.2 Example
Make a change in the original dictionary, and see that the items list gets updated as well:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.items()
print(x) #before the change
car["year"] = 2020
print(x) #after the change
2.4.3 Example
Add a new item to the original dictionary, and see that the items list gets updated as well:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.items()
print(x) #before the change
car["color"] = "red"
print(x) #after the change
2.5 Check if Keys Exists
To determine if a specified key is present in a dictionary use the in
keyword:
2.5.1 Example
Check if "model" is present in the dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary")