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'}
Căutare
Categorii
Citeste mai mult
Money
How to Make Money Online
How to Make Money Online In today’s digital age, making money online has become more...
By Leonard Pokrovski 2025-09-20 17:34:15 0 5K
Business
Are There Examples of Effective Business Biographies?
Business biographies serve as a fascinating lens through which we can explore the journeys of...
By Dacey Rankins 2025-01-06 15:49:49 0 14K
Социальные проблемы
Последний Парадизо. The Last Paradiso. (2021)
Италия, 1950-е.Свободолюбивый фермер борется за лучшие условия труда.Он мечтает о любви,...
By Nikolai Pokryshkin 2022-09-16 18:07:46 0 26K
Agriculture
Three types of Rocks
The 3 Different Names Of Rocks With Examples  Whether you’re an avid rockhound or...
By FWhoop Xelqua 2023-05-13 21:08:36 0 24K
Lifestyle Choices
How to work less and get more: 4 main mistakes of all workaholics
Many of us grew up with the belief that you can only make a lot of money by working hard and...
By FWhoop Xelqua 2023-02-19 16:40:49 0 21K

BigMoney.VIP Powered by Hosting Pokrov