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'}
Pesquisar
Categorias
Leia Mais
Outro
FYRE: The Greatest Party That Never Happened. (2019)
An exclusive behind the scenes look at the infamous unraveling of the Fyre music festival. My...
Por Leonard Pokrovski 2023-06-04 19:53:08 0 30K
Marketing and Advertising
What Is the Customer Acquisition Funnel?
The customer acquisition funnel is one of the most fundamental frameworks in marketing and growth...
Por Dacey Rankins 2026-01-19 15:00:59 0 405
Personal Finance
Should I choose federal or private student loans?
Should I choose federal or private student loans? Short answer: Start with federal loans. For...
Por Leonard Pokrovski 2025-12-16 21:10:40 0 3K
Finance
What Is Foreclosure?
What Is Foreclosure? Foreclosure is a legal process that occurs when a homeowner fails to make...
Por Leonard Pokrovski 2025-10-05 19:27:12 0 5K
Child Health
Only a day. The expert told what disease 63% of children die from
Only a day. The expert told what disease 63% of children die from In developed countries,...
Por Leonard Pokrovski 2024-04-11 22:50:22 0 14K

BigMoney.VIP Powered by Hosting Pokrov