Python Dictionary Key/Value Swap

0
6K

You can change the keys and values in a dictionary using a loop and items(). This can be helpful if the key doesn't exist in the dictionary, but the value exist. Here's an example:

d = {'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}

d_swap = {v: k for k, v in d.items()}
print(d_swap)
# {'val1': 'key1', 'val2': 'key2', 'val3': 'key3'}
Search
Categories
Read More
Социальные проблемы
Король Стейтен-Айленда. The King of Staten Island. (2020)
24-летний инфантил по имени Скотт живёт с мамой и младшей сестрой в нью-йоркском районе...
By Nikolai Pokryshkin 2022-10-16 17:00:48 0 22K
Mental Health
Schizophrenia Differential Diagnosis
Differential diagnosisTo make a diagnosis of schizophrenia other possible causes of psychosis...
By Kelsey Rodriguez 2023-02-03 17:25:53 0 7K
Team Spirit
Team Spirit
In team sports, the coach is faced with the task of creating a good team and nurturing team...
By FWhoop Xelqua 2023-01-15 11:06:07 0 13K
History
Paths of Glory. (1957)
After refusing to attack an enemy position, a general accuses the soldiers of cowardice and their...
By Leonard Pokrovski 2022-12-17 19:54:32 0 15K
Телевидение
Телеканал 360 Новости прямая трансляция.
«360 Новости» — круглосуточный информационный телеканал, на котором зрители...
By Nikolai Pokryshkin 2022-11-22 15:11:04 0 20K
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov