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
Social Issues
Project Power. (2020)
When a pill that gives its users unpredictable superpowers for five minutes hits the streets of...
By Leonard Pokrovski 2022-10-17 19:17:36 0 30K
Business
What Is the Difference Between Licensing and Franchising?
Two entrepreneurs sit across a conference table. Both want growth. Both own valuable...
By Dacey Rankins 2026-06-12 17:01:05 0 1K
Economics
What is a carbon tax?
What Is a Carbon Tax? A refinery in Texas exhales more carbon dioxide before dawn than a forest...
By Leonard Pokrovski 2026-05-22 21:27:42 0 4K
Marketing and Advertising
B2B Marketing Often Targets a Buying Group—You Have to Define Each Member and Their Influence
Unlike B2C marketing, where a single individual often makes the purchase decision, B2B buying is...
By Dacey Rankins 2025-09-20 15:51:55 0 17K
Business
What Are the Key Features of PaaS?
The first time I walked into a software team's weekly planning meeting, I expected to hear...
By Dacey Rankins 2026-07-03 22:49:43 0 317

BigMoney.VIP Powered by Hosting Pokrov