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'}
Cerca
Categorie
Leggi tutto
Жизненные вопросы
1+1. The Intouchables. (2011)
Пострадав в результате несчастного случая, богатый аристократ Филипп нанимает в помощники...
By Nikolai Pokryshkin 2022-12-05 20:58:24 0 43K
Business
How Do I Come Up with a Viable Startup Idea?
Starting a new business can be an exciting yet daunting endeavor. One of the first and most...
By Dacey Rankins 2025-03-24 15:17:00 0 10K
Nutrition
The Importance of Nutrition for Health and Well-Being
Nutrition is the science that explores how food impacts health, growth, and development. It plays...
By Dacey Rankins 2024-10-18 18:46:16 0 20K
News and Media
2600: The Hacker Quarterly
2600: The Hacker Quarterly is an American magazine specializing in the publication of technical...
By FWhoop Xelqua 2023-04-15 18:16:02 0 23K
Телевидение
Телеканал Саратов 24 прямая трансляция.
"Саратов 24" — начал вещание 14 ноября 2014 года. Это уникальный формат телевидения,...
By Nikolai Pokryshkin 2022-11-07 11:09:07 0 25K

BigMoney.VIP Powered by Hosting Pokrov