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
Social Issues
The Great Dictator. (1940)
Dictator Adenoid Hynkel tries to expand his empire while a poor Jewish barber tries to avoid...
By Leonard Pokrovski 2022-12-21 19:24:05 0 21K
Business
What Is User Behavior Modeling and How Is It Applied?
In today’s digital ecosystem, companies collect massive amounts of data on how users...
By Dacey Rankins 2025-08-25 17:11:49 0 3K
Media
Startup.com. (2001)
Traces the birth and failure of new media company govWorks.com. My Link
By Leonard Pokrovski 2023-06-08 19:46:32 0 31K
Business
How to choose the perfect outfit for the office: style tips
Choosing the perfect outfit for the office can be challenging, especially if you want to look...
By Dacey Rankins 2024-09-24 13:32:15 0 15K
Mental Health
Dyslexia: Gene–environment interaction
The contribution of gene–environment interaction to reading disability has been intensely...
By Kelsey Rodriguez 2023-06-22 18:13:18 0 13K

BigMoney.VIP Powered by Hosting Pokrov