Python Dictionary Key/Value Swap

0
13K

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'}
Cerca
Categorie
Leggi tutto
Media
Dial M for Murder. (1954)
A former tennis star arranges the murder of his adulterous wife. My Link
By Leonard Pokrovski 2023-03-07 19:29:16 0 23K
Business
What Are Common Mistakes in Product Planning?
Product planning is one of the most critical responsibilities in product management. A strong...
By Dacey Rankins 2025-08-20 15:41:09 0 6K
Personal Finance
What Tax Credits Am I Eligible For?
What Tax Credits Am I Eligible For? A Guide to the EITC, Child Tax Credit, EV Credit, and More...
By Leonard Pokrovski 2025-12-25 20:24:11 0 2K
Business
Brilliant performance
I know charismatic, confident people who avoid even making a toast at a family holiday. They they...
By Dacey Rankins 2024-09-13 18:20:04 0 15K
Mental Health
Dementia: Vascular
Vascular dementia accounts for at least 20% of dementia cases, making it the second most common...
By Kelsey Rodriguez 2023-07-26 19:30:19 0 17K

BigMoney.VIP Powered by Hosting Pokrov