To give an item a new value, refer to it by its key name:
Example
Set "year" to 2018:
Prompt
Write a Python program that creates a dictionary called thisdict with keys "brand", "model", and "year" with values "Ford", "Mustang", and 1964. Then change the "year" value to 2018 and print the dictionary.
Output: complete Python program
main.py
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["year"] = 2018
print(thisdict)
Update Dictionary
Calling update() copies the items of its argument into the dictionary.
That argument has to be either a dictionary or an iterable object made up of key:value pairs.
Example
Give the car a new "year" through update():
Prompt
Write a Python program that creates a dictionary called thisdict with keys "brand", "model", and "year" with values "Ford", "Mustang", and 1964. Then update the "year" to 2020 using the update() method and print the dictionary.
Output: complete Python program
main.py
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.update({"year": 2020})
print(thisdict)