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
Business
What Is a Business Biography?
A business biography is a detailed account of a person’s career and professional...
By Dacey Rankins 2024-12-27 15:56:10 0 16K
Business
What Tools or Techniques Support Product Planning?
Product planning is the backbone of effective product management. It provides clarity on what...
By Dacey Rankins 2025-08-20 15:53:01 0 3K
Horror
The Twilight Saga: Breaking Dawn - Part 2. (2012)
After the birth of Renesmee/Nessie, the Cullens gather other vampire clans in order to protect...
By Leonard Pokrovski 2023-05-19 20:10:22 0 34K
Financial Services
Beyond salary: benefits may matter more than you think
So you’ve just gotten your first job offer. While salary may be your main focus,...
By Mark Lorenzo 2023-05-29 19:36:13 0 23K
Mental Health
Autism Spectrum: Alternative medicine
A multitude of researched alternative therapies have also been implemented. Many have resulted in...
By Kelsey Rodriguez 2023-03-17 18:19:08 0 14K

BigMoney.VIP Powered by Hosting Pokrov