Changing Items in a Dictionary in Python: A Quick Guide
Learn how to update the value of an item in a Python dictionary by referencing its key. This guide provides step-by-step examples to help you modify dictionary entries effectively.
Change Dictionary Items
You can change the value of a specific item in a dictionary by referring to its key name:
Example
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["year"] = 2018
print(thisdict["year"])
Output
2018
Update Dictionary
The update()
method will update the dictionary with the items from the given argument.
Example
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.update({"year": 2020})
print(thisdict)
Output
{'brand': 'Ford', 'model': 'Mustang', 'year': 2020}