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'}
Zoeken
Categorieën
Read More
Life Issues
The Gold Rush. (1925)
A prospector goes to the Klondike during the 1890s gold rush in hopes of making his fortune, and...
By Leonard Pokrovski 2023-03-19 18:45:00 0 22K
Alternative Medicine
Alternative Medicine: Exploring Holistic Approaches to Health and Wellness
Alternative medicine, also known as complementary or integrative medicine, refers to a wide...
By Dacey Rankins 2024-11-21 16:01:27 0 14K
Tennis
Tennis: The Timeless Sport of Skill, Strategy, and Endurance
Tennis: The Timeless Sport of Skill, Strategy, and Endurance Tennis, a sport beloved for its...
By Leonard Pokrovski 2024-07-11 13:43:40 0 25K
Business
What Is Growth Hacking?
In today’s fast-paced digital landscape, businesses are under constant pressure to grow...
By Dacey Rankins 2025-09-11 16:25:46 0 3K
Business
How Do I Come Up with a Startup Idea?
One of the most critical steps in launching a successful business is coming up with a compelling...
By Dacey Rankins 2025-04-07 17:29:29 0 8K

BigMoney.VIP Powered by Hosting Pokrov