Python Dictionary Key/Value Swap

0
14K

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
Decision Making and Problem Solving
What activities improve creativity?
What Activities Improve Creativity? Creativity Does Not Arrive When Summoned A blank page sits...
By Michael Pokrovski 2026-06-11 19:56:31 0 389
Desktop Publishing
Desktop Publishing
Desktop publishing system (NIS) is a set of equipment for preparing the original layout of the...
By FWhoop Xelqua 2023-03-04 17:57:41 0 24K
Productivity
How to stay motivated to achieve goals?
Motivation is the most misunderstood currency in the world of productivity. We treat it like a...
By Michael Pokrovski 2026-04-30 18:22:05 0 1K
Television
UN press briefings. Live TV. USA.
The UN Web TV Channel is available 24 hours a day with selected live programming of United...
By Nikolai Pokryshkin 2022-10-12 19:01:24 0 32K
Programming
Chrome Extensions Using Python
The idea is to compile Python to Javascript (technically a JS pre-compiler)...
By Jesse Thomas 2023-03-20 20:46:37 0 13K

BigMoney.VIP Powered by Hosting Pokrov