Python Dictionary Key/Value Swap

0
13K

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
Жизненные вопросы
Токийская повесть. Tokyo Story. (1953)
Пожилая супружеская пара едет из провинции в Токио навестить детей: сына, ставшего доктором, и...
By Nikolai Pokryshkin 2023-04-13 10:36:07 0 31K
Sport
Touching the Void (2003)
The true story of two climbers and their perilous journey up the west face of Siula Grande in the...
By Leonard Pokrovski 2023-07-19 22:32:38 0 23K
Business
What is financial planning, why is it needed and how to do it correctly
What is financial planning, why is it needed and how to do it correctly Business...
By Leonard Pokrovski 2024-07-18 18:26:39 0 28K
Economics
How does comparative economics analyze economic performance?
Comparative economics is the branch of economics that studies how different economic systems and...
By Leonard Pokrovski 2026-03-04 21:38:48 0 5K
Economics
What Is the Relationship Between Inequality and Development?
What Is the Relationship Between Inequality and Development? The relationship between inequality...
By Leonard Pokrovski 2026-04-11 17:45:59 0 748

BigMoney.VIP Powered by Hosting Pokrov