1# Basic syntax:
2# Approach 1:
3dictionary[new_key] = dictionary[old_key]
4del dictionary[old_key]
5
6# Approach 2:
7dictionary[new_key] = dictionary.pop(old_key)
1my_dict = {
2 'foo': 42,
3 'bar': 12.5
4}
5my_dict['foo'] = "Hello"
6print(my_dict['foo'])
7#This will give the output:
8
9'Hello'
1>>> dictionary = { 1: 'one', 2:'two', 3:'three' }
2>>> dictionary['ONE'] = dictionary.pop(1)
3>>> dictionary
4{2: 'two', 3: 'three', 'ONE': 'one'}
5>>> dictionary['ONE'] = dictionary.pop(1)
6Traceback (most recent call last):
7 File "<input>", line 1, in <module>
8KeyError: 1
1d = {1: "one", 2: "three"}
2d1 = {2: "two"}
3
4# updates the value of key 2
5d.update(d1)
6print(d)
7
8d1 = {3: "three"}
9
10# adds element with key 3
11d.update(d1)
12print(d)