Python Dictionary Key/Value Swap

0
11K

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'}
Căutare
Categorii
Citeste mai mult
Environment
Poison Dart Frogs
Poison dart frog (also known as dart-poison frog, poison frog or formerly known as poison arrow...
By FWhoop Xelqua 2023-04-23 16:51:05 0 15K
Business
How Does Page Speed Affect Conversion Rates?
In today’s digital-first world, users expect instant gratification. Whether they’re...
By Dacey Rankins 2025-09-10 15:09:48 0 2K
Tools
Tools used to rennovate
If the Fixies really existed, they would be the most welcome guests in every home. However, they...
By FWhoop Xelqua 2023-05-14 21:08:16 0 22K
Жизненные вопросы
Джокер. Joker. (2019)
Готэм, начало 1980-х годов. Комик Артур Флек живет с больной матерью, которая с детства учит его...
By Nikolai Pokryshkin 2022-12-30 10:49:32 0 30K
Media
World News Agencies
ABYZ News Links Directory links to more than 15,000 newspapers and other news sites such as news...
By FWhoop Xelqua 2022-11-06 16:45:16 0 22K

BigMoney.VIP Powered by Hosting Pokrov