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'
1a_dict = {"a": 1, "B": 2, "C": 3}
2
3new_key = "A"
4old_key = "a"
5a_dict[new_key] = a_dict.pop(old_key)
6
7print(a_dict)
8OUTPUT
9{'B': 2, 'C': 3, 'A': 1}
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