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'}
Search
Categories
Read More
Nature and Science
Tom & Jerry (2021)
A chaotic battle ensues between Jerry Mouse, who has taken refuge in the Royal Gate Hotel, and...
By Leonard Pokrovski 2022-09-19 18:44:26 0 31K
Home Automation
Finding Investors for the Smart Base for TV: A Revolutionary Home Entertainment Solution
In the ever-evolving landscape of home entertainment, innovation is key. The Smart Base for TV...
By Vladimir Tsigrik 2024-11-01 17:10:07 0 17K
Жизненные вопросы
Жить. Ikiru. (1952)
Старик узнаёт, что жить ему осталось недолго и решает, что детская площадка на месте зловонного...
By Nikolai Pokryshkin 2023-01-19 11:36:02 0 27K
Business
Frequency of One-on-One Meetings: Do I Have to Run Weekly 1:1s?
How Often Is Often Enough? One-on-one meetings (1:1s) are among the most important tools for...
By Dacey Rankins 2025-08-01 14:09:35 0 6K
Business
How Do Influencers Maintain Authenticity with Their Audience?
In the world of influencer marketing, authenticity is everything. Audiences crave genuine,...
By Dacey Rankins 2025-02-27 17:13:16 0 13K

BigMoney.VIP Powered by Hosting Pokrov