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
Arts, Culture and Entertainment
Singin' in the Rain. (1952)
A silent film star falls for a chorus girl just as he and his delusionally jealous screen partner...
By Leonard Pokrovski 2023-01-08 20:06:40 1 23K
Marketing and Advertising
How Does Mobile Behavior Differ from Desktop Behavior?
The way people interact with digital content has undergone a seismic shift over the past decade....
By Dacey Rankins 2025-10-20 16:03:47 0 739
Marketing and Advertising
Do I Need a Website to Start Affiliate Marketing?
When people first hear about affiliate marketing, their assumption is often that they’ll...
By Dacey Rankins 2025-09-18 16:05:01 0 4K
Financial Services
Elasticity in areas other than price
Key points Elasticity is a general term, referring to percentage change of one...
By Mark Lorenzo 2023-07-07 20:37:00 0 13K
Financial Services
Decisions within a budget constraint
Key points The budget constraint is the boundary of the opportunity...
By Mark Lorenzo 2023-04-19 19:48:00 0 13K

BigMoney.VIP Powered by Hosting Pokrov