Tukar kunci dan nilai -nilai dalam Kamus Python
my_dict = {'x':1, 'y':2, 'z':3}
my_swapped_dict = {v: k for k, v in my_dict.items()}
#{1: 'x', 2: 'y', 3: 'z'}
CodeHunter
my_dict = {'x':1, 'y':2, 'z':3}
my_swapped_dict = {v: k for k, v in my_dict.items()}
#{1: 'x', 2: 'y', 3: 'z'}
old_dict = {'A': 67, 'B': 23, 'C': 45, 'D': 56, 'E': 12, 'F': 69, 'G': 67, 'H': 23}
new_dict = dict([(value, key) for key, value in old_dict.items()])
print(new_dict)
for i in new_dict:
print(i, " : ", new_dict[i])
my_dict = {2:3, 5:6, 8:9}
new_dict = {}
for k, v in my_dict.items():
new_dict[v] = k
dict = {value:key for key, value in dict.items()}
d={1:'Monday',2:'Tuesday',3:'Wednesday',\
4:'Thursday',5:'Friday',6:'Satuday',7:'Sunday'}
swap_d = {value: key for key, value in d.items()}
print(swap_d)