Accessing Dictionary Items in Python: A Quick Guide
Learn how to access items in a Python dictionary using keys. This guide demonstrates how to retrieve values by referring to their keys within square brackets, with practical examples to illustrate the process.
Accessing Dictionary Items
You can access the items of a dictionary by referring to its key name, inside square brackets:
Example
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]
print(x)
Output
Mustang
There is also a method called get()
that will give you the same result:
Example
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict.get("model")
print(x)
Output
Mustang
Get Keys
The keys()
method will return a list of all the keys in the dictionary.
Example
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict.keys()
print(x)
Output
dict_keys(['brand', 'model', 'year'])
The list of keys is a view of the dictionary, meaning that any changes done to the dictionary will be reflected in the keys list.
Example
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.keys()
print(x) # before the change
car["color"] = "white"
print(x) # after the change
Output
dict_keys(['brand', 'model', 'year'])
dict_keys(['brand', 'model', 'year', 'color'])
Get Values
The values()
method will return a list of all the values in the dictionary.
Example
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict.values()
print(x)
Output
dict_values(['Ford', 'Mustang', 1964])
The list of values is a view of the dictionary, meaning that any changes done to the dictionary will be reflected in the values list.
Example
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.values()
print(x) # before the change
car["year"] = 2020
print(x) # after the change
Output
dict_values(['Ford', 'Mustang', 1964])
dict_values(['Ford', 'Mustang', 2020])
Example
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.values()
print(x) # before the change
car["color"] = "red"
print(x) # after the change
Output
dict_values(['Ford', 'Mustang', 1964])
dict_values(['Ford', 'Mustang', 1964, 'red'])
Get Items
The items()
method will return each item in a dictionary, as tuples in a list.
Example
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict.items()
print(x)
Output
dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])
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.
Example
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.items()
print(x) # before the change
car["year"] = 2020
print(x) # after the change
Output
dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])
dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 2020)])
Example
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.items()
print(x) # before the change
car["color"] = "red"
print(x) # after the change
Output
dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])
dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964), ('color', 'red')])
Check if Key Exists
To determine if a specified key is present in a dictionary, use the in
keyword:
Example
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary")
Output
Yes, 'model' is one of the keys in the thisdict dictionary