What is dictionary in Python?
Dictionary is used to store a collection of elements/items. In Python, The other 3 of them in Python are Tuple, Set, and List.
Dictionaries are used to store data in a format of key:value. Dictionaries have the following few characteristics.
- (1) Dictionaries are ordered, as of Python Version 3.7. In Python 3.6 or earlier, dictionaries are unordered.
- (2) Items in a Dictoinary are changeable.
- (3) Dictionary does not allow duplicates.
Define a Dictionary
How can you create a Python dictionary? The following code shows how you can define a dictionary in Python by specifying Keys and Values.
Tesla_dict = {
"brand": "Tesla",
"model": "Model 3",
"year": 2022
}
print(Tesla_dict)
{'brand': 'Tesla', 'model': 'Model 3', 'year': 2022}
Index and Print Dictionary
Instead of print out all the items, you can also choose a certain pair to print out. You can index the Key, and then it can print out the Value related to the key.
print(Tesla_dict["model"])
Model 3
Note that, a key can have more than one value. The following code example shows how can have multiple values under the same key. That is, you can just put multiple values within the same square bracket.
d = {'brands': ['Tesla', 'Toyota'], 'models': ["Model 3", "RAV4"]}
print(d)
{'brands': ['Tesla', 'Toyota'], 'models': ['Model 3', 'RAV4']}
Change Items in Dictionary
We can change the value of a dictionary by referring to its key. The following code shows an example.
Tesla_dict = {
"brand": "Tesla",
"model": "Model 3",
"year": 2021
}
print(Tesla_dict)
Tesla_dict["year"] = 2022
print(Tesla_dict)
The following code shows the result showing before and after the change.
{'brand': 'Tesla', 'model': 'Model 3', 'year': 2021} {'brand': 'Tesla', 'model': 'Model 3', 'year': 2022}
We can also use the updated method to change the item in a dictionary. The following is the example.
Tesla_dict = {
"brand": "Tesla",
"model": "Model 3",
"year": 2021
}
print(Tesla_dict)
Tesla_dict.update({"year":2022})
print(Tesla_dict)
{'brand': 'Tesla', 'model': 'Model 3', 'year': 2021} {'brand': 'Tesla', 'model': 'Model 3', 'year': 2022}
Remove Items in Dictionary
We can also delete some items in a dictionary. There are a few methods that we can use. The first one is use the pop() method to remove specific items in the dictionary. The following is the code example.
Tesla_dict = {
"brand": "Tesla",
"model": "Model 3",
"year": 2021
}
print(Tesla_dict)
Tesla_dict.pop("model")
print(Tesla_dict)
The following is the output showing the difference between before using pop() and after.
{'brand': 'Tesla', 'model': 'Model 3', 'year': 2021} {'brand': 'Tesla', 'year': 2021}
You can also use popitem() method to remove the latest item in the dictionary. Since this method is related to orders in a dictionary, Python 3.6 or earlier version will randomly remove items. In comparison, we can see below that it removes the last item in the dictionary.
Tesla_dict = {
"brand": "Tesla",
"model": "Model 3",
"year": 2021
}
print(Tesla_dict)
Tesla_dict.popitem()
print(Tesla_dict)
{'brand': 'Tesla', 'model': 'Model 3', 'year': 2021} {'brand': 'Tesla', 'model': 'Model 3'}
Another one is to use Del to remove items in a dictionary.
Tesla_dict = {
"brand": "Tesla",
"model": "Model 3",
"year": 2021
}
print(Tesla_dict)
del Tesla_dict["brand"]
print(Tesla_dict)
{'brand': 'Tesla', 'model': 'Model 3', 'year': 2021} {'model': 'Model 3', 'year': 2021}
Finally, you can also use clear() to clear all the items in a dictionary. The following is the example.
Tesla_dict = {
"brand": "Tesla",
"model": "Model 3",
"year": 2021
}
print(Tesla_dict)
Tesla_dict.clear()
print(Tesla_dict)
The following is the output showing before and after using clear().
{'brand': 'Tesla', 'model': 'Model 3', 'year': 2021} {}