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'}
Site içinde arama yapın
Kategoriler
Read More
Animal
"Veterinary medicine is exciting!"
"Veterinary medicine is exciting!" Veterinary medicine is a field of scientific knowledge and...
By Leonard Pokrovski 2024-04-10 21:53:12 0 20K
Marketing and Advertising
What Is the Difference Between Boosting a Post and Creating an Ad on Facebook?
Introduction Facebook remains one of the most powerful digital advertising platforms for...
By Dacey Rankins 2025-10-07 14:49:03 0 3K
Arts, Culture and Entertainment
The Prom (2020)
In desperate need of a noble cause to revive their public images and bounce back, self-centred...
By Leonard Pokrovski 2022-09-26 20:48:09 0 28K
Horror
Jaws. (1975)
When a killer shark unleashes chaos on a beach community off Cape Cod, it's up to a local...
By Leonard Pokrovski 2023-04-11 18:50:31 0 25K
Business
25 Most Common Job Interview Questions and Answers [2025]
Preparing for a job interview can feel overwhelming—especially in 2025, where expectations...
By Dacey Rankins 2025-06-23 14:39:05 0 12K

BigMoney.VIP Powered by Hosting Pokrov