Code 1:
kv = {'a': 'A', 'b': 'B', 'c': 'C'}
d = {'a': 1, 'b': 2, 'c': 3}
for key in d.keys():
d[kv[key]] = d.pop(key)
print(d)
Output:
{'A': 1, 'B': 2, 'C': 3}
The above example works fine
But, when we increase the number of elements in the dictionary,
it raises KeyError as shown in example below
Code 2:
kv = {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}
d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
for key in d.keys():
d[kv[key]] = d.pop(key)
print(d)
Output:
Traceback (most recent call last):
File "C:\Users\user\OneDrive\Documents\VSCode_Python\new.py", line 11, in <module>
d[kv[key]] = d.pop(key)
KeyError: 'A'