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
News and Media
2600: The Hacker Quarterly
2600: The Hacker Quarterly is an American magazine specializing in the publication of technical...
By FWhoop Xelqua 2023-04-15 18:16:02 0 24K
Economics
How Does International Trade Affect Economic Development?
How Does International Trade Affect Economic Development? International trade plays a crucial...
By Leonard Pokrovski 2026-03-13 22:22:35 0 8K
Business
What office supplies should I buy first?
It starts innocently. You open a new tab. Search for “office supplies.” Add a few...
By Dacey Rankins 2026-05-04 19:40:49 0 3K
Social Issues
Calm with Horses.(2020)
Douglas 'Arm' Armstrong has become the feared enforcer for the drug-dealing Devers family, whilst...
By Leonard Pokrovski 2022-10-27 22:11:43 0 34K
Marketing and Advertising
What Is the Best Time to Run Radio Ads?
Choosing the right time to run your radio ads is just as important as writing a great script or...
By Dacey Rankins 2026-02-06 19:34:50 0 4K

BigMoney.VIP Powered by Hosting Pokrov