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
Television
Fox 4 Live TV Kansas City, Missouri, USA.
WDAF-TV, virtual channel 4 (UHF digital channel 34), is a FOX-affiliated television station...
By Nikolai Pokryshkin 2022-10-20 09:53:37 0 32K
Social Issues
Gifted. (2017)
Frank, a single man raising his child prodigy niece Mary, is drawn into a custody battle with his...
By Leonard Pokrovski 2023-05-18 19:45:37 0 33K
Mental Health
ADHD: In adults
Adults with ADHD are diagnosed under the same criteria, including that their signs must have been...
By Kelsey Rodriguez 2023-04-24 16:51:27 0 11K
Mental Health
Dementia: Pre-dementia
Pre-dementia states include pre-clinical and prodromal stages. The prodromal stages includes (1)...
By Kelsey Rodriguez 2023-07-17 17:52:05 0 15K
Business
What Skills Are Required to Be a Product Marketer?
Product marketing has emerged as one of the most dynamic and cross-functional roles in modern...
By Dacey Rankins 2025-09-13 19:10:12 0 2K

BigMoney.VIP Powered by Hosting Pokrov