Python Dictionary Key/Value Swap

0
11KB

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'}
Rechercher
Catégories
Lire la suite
Social Issues
Monster (2021)
Monster tells the story of Steve Harmon (Kelvin Harrison, Jr.) a seventeen-year-old honor student...
Par Leonard Pokrovski 2022-09-07 13:37:58 0 34KB
Mental Health
Autism Spectrum - Early Life
Early lifeSeveral prenatal and perinatal complications have been reported as possible risk...
Par Kelsey Rodriguez 2023-02-22 17:19:57 0 13KB
Illustration
How to earn money through illustration
What will you need to make money on drawings on the Internet? Before you learn how to make money...
Par FWhoop Xelqua 2022-10-04 14:57:43 0 32KB
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...
Par Dacey Rankins 2024-09-24 13:32:15 0 15KB
Business
How Can Businesses Recover After a Crisis?
Crisis is an inevitable part of any business journey. Whether it’s a financial downturn, a...
Par Dacey Rankins 2025-01-27 20:43:22 0 16KB

BigMoney.VIP Powered by Hosting Pokrov