I have a small example of exchanging a key and one of its values, but I did not make it work:
dict1 = {1: ['BB', 'CC', 'DD'], 2: ['FF', 'HH', 'GG']}
print(dict1)
for key in dict1:
for value in dict1[key]:
if value == 'BB':
temp = key
key = value
value = temp
print(dict1)
Current output:
{1: ['BB', 'CC', 'DD'], 2: ['FF', 'HH', 'GG']}
Desired output:
{BB: [1, 'CC', 'DD'], 2: ['FF', 'HH', 'GG']}
I have used the temp
to exchange the value
and key
, but why does the output does not change (keeps as same as the original dict1
)?