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
Internet
History of the Internet
History of the Internet The Internet is a worldwide computer network, the many nodes of...
By Leonard Pokrovski 2024-03-25 22:10:13 0 29K
Economics
What Are the Main Goals of Development Economics?
What Are the Main Goals of Development Economics? Development economics is a branch of economics...
By Leonard Pokrovski 2026-03-09 22:51:20 0 6K
Marketing and Advertising
How Do Ads Make Money?
Advertising is one of the largest industries in the world, generating hundreds of billions of...
By Dacey Rankins 2026-01-05 23:54:49 0 5K
Programming
Python Autocomplete Plugin
Making a functional program using Python can be stressful since you would need to go back and...
By Jesse Thomas 2023-01-30 17:07:34 0 13K
Productivity
How do I eliminate distractions?
How Do I Eliminate Distractions? Distractions are one of the primary barriers to sustained...
By Michael Pokrovski 2026-04-18 05:54:19 0 3K

BigMoney.VIP Powered by Hosting Pokrov