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
Marketing and Advertising
What Metrics / KPIs Should Be Used in Mobile Marketing?
Introduction In the rapidly evolving world of mobile marketing, success isn’t just about...
By Dacey Rankins 2025-10-17 15:18:48 0 3K
Sport
Warrior. (2011)
The youngest son of an alcoholic former boxer returns home, where he's trained by his father for...
By Leonard Pokrovski 2023-03-12 17:31:43 0 24K
Personal Finance
What Is a Good Credit Score?
What Is a Good Credit Score? Your credit score is one of the most important numbers in your...
By Leonard Pokrovski 2025-10-24 11:17:54 0 2K
Mental Health
Psychosis: Psychoactive Drugs
Various psychoactive substances (both legal and illegal) have been implicated in causing,...
By Kelsey Rodriguez 2023-05-29 18:19:02 0 12K
Mental Health
Dementia: Causes
Many causes of dementia are neurodegenerative, and protein misfolding is a cardinal feature of...
By Kelsey Rodriguez 2023-07-24 17:20:23 0 17K

BigMoney.VIP Powered by Hosting Pokrov