Python Dictionary Key/Value Swap

0
14K

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
Социальные проблемы
Бен-Гур. Ben-Hur. (1959)
Действие фильма начинается в Палестине в 26 году от Рождества Христова. Два друга, иудей Бен Гур...
By Nikolai Pokryshkin 2023-03-23 18:08:29 0 42K
Business
How Do I Measure or Know if My Communication Skills Are Good?
Introduction Communication is one of the most essential skills in academic life, personal...
By Dacey Rankins 2025-11-21 15:47:29 0 6K
Marketing and Advertising
Measuring the Effectiveness of Storytelling in Marketing
You’ve crafted a compelling story — authentic, emotional, and aligned with your...
By Dacey Rankins 2025-11-04 21:13:03 0 7K
Social Issues
Tenet(2020)
Armed with only one word, Tenet, and fighting for the survival of the entire world, a Protagonist...
By Leonard Pokrovski 2022-10-16 13:51:00 0 28K
Financial Services
Price ceilings and price floors
Key points Price ceilings prevent a price from rising above a certain level....
By Mark Lorenzo 2023-04-17 20:06:12 0 14K

BigMoney.VIP Powered by Hosting Pokrov