Python Dictionary Key/Value Swap

0
14K

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'}
Search
Categories
Read More
Programming
Python setup.py
setup.py is a python file, the presence of which is an indication that the module/package...
By Jesse Thomas 2023-04-26 21:12:26 0 15K
Camps
Embracing Adventure: The Magic of Recreational Camping
Embracing Adventure: The Magic of Recreational Camping Introduction: Amidst the hustle and...
By Leonard Pokrovski 2024-05-09 10:10:04 0 20K
Decision Making and Problem Solving
How do children develop creativity?
How Do Children Develop Creativity? Watch a child with an empty cardboard box. Not a toy. Not...
By Michael Pokrovski 2026-06-24 20:27:55 0 2K
Personal Finance
How Can I Keep Track of My Spending?
How Can I Keep Track of My Spending? For Those Trying to Budget or Reduce Unnecessary Expenses...
By Leonard Pokrovski 2025-11-12 16:14:39 0 17K
Economics
Why do companies pollute?
Why Do Companies Pollute? There is a photograph taken from orbit at night of the Gulf Coast...
By Leonard Pokrovski 2026-05-22 21:18:52 0 3K

BigMoney.VIP Powered by Hosting Pokrov