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
Life Issues
Tangled. (2010)
The magically long-haired Rapunzel has spent her entire life in a tower, but now that a runaway...
Por Leonard Pokrovski 2023-04-29 18:06:15 0 26K
Stories
Zeitgeist (2007)
Mythology and belief in society today, presenting uncommon perspectives of common cultural...
Por Leonard Pokrovski 2023-07-15 20:09:29 0 29K
Business
What Are the Company's Core Values? Ensuring Candidates' Values Align with the Company's Culture
When candidates step into a job interview, they often focus on skills, experience, and...
Por Dacey Rankins 2025-06-19 18:45:03 0 11K
Business
Startup from A to Z: How to Turn an Idea into a Successful Business
Startup from A to Z: How to Turn an Idea into a Successful Business Perhaps each of us at...
Por Leonard Pokrovski 2024-08-01 22:33:19 0 28K
Personal Finance
Can I Pay Off My Loan Early? What Happens If I Repay Ahead of Schedule?
  Can I Pay Off My Loan Early? What Happens If I Repay Ahead of Schedule? Managing a loan...
Por Leonard Pokrovski 2025-11-06 19:20:01 0 8K

BigMoney.VIP Powered by Hosting Pokrov