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
Business
When Should a Company Use IaaS?
Technology leaders rarely struggle with a shortage of options. The challenge is usually the...
By Dacey Rankins 2026-06-05 19:22:29 0 1K
Decision Making and Problem Solving
How can I generate better ideas and become more creative?
How Can I Generate Better Ideas and Become More Creative? There is a strange moment that happens...
By Michael Pokrovski 2026-06-20 19:07:41 0 3K
Business
Project Management Fundamentals: What Should You Focus On?
Project Management Fundamentals: What Should You Focus On?   "Focus on...
By Leonard Pokrovski 2024-08-11 15:30:50 0 20K
Социальные проблемы
Удалить историю. Delete History. (2020)
Жизнь троих обитателей городка подчинена новейшим технологиям и опутана социальными сетями....
By Nikolai Pokryshkin 2022-10-15 21:52:58 1 29K
Economics
What products are most exported?
What Products Are Most Exported? The Hidden Logic Behind Global Trade Every morning, before the...
By Leonard Pokrovski 2026-06-25 23:27:18 0 1K

BigMoney.VIP Powered by Hosting Pokrov