Check if Key Exists :
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
Change Dictionary Value :
thisdict["year"] = 2018
print(thisdict)
Output :
{'brand': 'Ford', 'model': 'Mustang', 'year': 2018}
Update Dictionary Value using update() :
thisdict.update({"year": 2020})
print(thisdict)
Output :
{'brand': 'Ford', 'model': 'Mustang', 'year': 2020}
Add New Item to Dictionary :
thisdict["color"] = "red"
print(thisdict)
Output :
{'brand': 'Ford', 'model': 'Mustang', 'year': 2020, 'color': 'red'}
Add Item using update() :
thisdict.update({"engine": "V8"})
print(thisdict)
Output :
{'brand': 'Ford', 'model': 'Mustang', 'year': 2020, 'color': 'red', 'engine': 'V8'}