Python Dictionary Key/Value Swap

0
12K

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
Other
Pirates of the Caribbean: The Curse of the Black Pearl (2003)
Blacksmith Will Turner teams up with eccentric pirate "Captain" Jack Sparrow to save his love,...
By Leonard Pokrovski 2023-06-23 19:53:50 0 33K
Жизненные вопросы
Яблоки. Apples. (2021)
В Афинах разгорается необычная эпидемия, люди внезапно теряют память. Жертвой становится и Арис...
By Nikolai Pokryshkin 2022-09-16 20:31:23 0 27K
Business
Mentoring FAQ – Common Questions About Mentoring
Mentoring is a powerful tool for personal and professional development, yet many people have...
By Dacey Rankins 2025-05-26 14:03:58 0 6K
Food
Best Foods for Weight Loss
Studies have shown that certain foods can affect appetite, so they may be beneficial for weight...
By FWhoop Xelqua 2022-09-21 14:00:25 0 29K
Life Issues
Aladdin (1992)
A kind-hearted street urchin and a power-hungry Grand Vizier vie for a magic lamp that has the...
By Leonard Pokrovski 2023-07-01 19:45:10 0 34K

BigMoney.VIP Powered by Hosting Pokrov