1d = {'key':'value'}
2print(d)
3# {'key': 'value'}
4d['mynewkey'] = 'mynewvalue'
5print(d)
6# {'mynewkey': 'mynewvalue', 'key': 'value'}
7
1thisdict = {
2 "brand": "Ford",
3 "model": "Mustang",
4 "year": 1964
5}
6thisdict["color"] = "red"
7print(thisdict)
1# to add key-value pairs to a dictionary:
2
3d1 = {
4 "1" : 1,
5 "2" : 2,
6 "3" : 3
7} # Define the dictionary
8
9d1["4"] = 4 # Add key-value pair "4" is key and 4 is value
10
11print(d1) # will return updated dictionary
1d = {'a': 1, 'b': 2}
2print(d)
3d['a'] = 100 # existing key, so overwrite
4d['c'] = 3 # new key, so add
5d['d'] = 4
6print(d)
1default_data = {'item1': 1,
2 'item2': 2,
3 }
4
5default_data.update({'item3': 3})
6# or
7default_data['item3'] = 3