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
Financial Services
Elasticity in the long run and short run
Key points In the market for goods and services, quantity supplied and quantity...
By Mark Lorenzo 2023-07-05 20:15:28 0 14K
Human Resources
How Much Do Headhunters Earn?
The profession of headhunting, also known as executive recruitment, has become increasingly...
By Dacey Rankins 2026-03-17 19:20:26 0 2K
Marketing and Advertising
How Do I Measure Outdoor Advertising Effectiveness?
Measuring outdoor advertising effectiveness has historically been one of the biggest challenges...
By Dacey Rankins 2026-02-02 18:11:29 0 5K
Business
What Do I Need to Begin Affiliate Marketing?
The internet has created a strange mythology around starting affiliate marketing. Depending on...
By Dacey Rankins 2026-05-15 16:21:40 0 211
Food and Related Products
Sushi
Sushi, or sushi (Jap. すし, 寿司, 壽司, 鮨, 鮓, 寿斗, 寿し sushi, [sɯɕi]; Indeclinable), is a dish of...
By FWhoop Xelqua 2023-04-26 17:00:48 0 18K

BigMoney.VIP Powered by Hosting Pokrov