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
Economics
How Does Income Inequality Affect Health?
How Does Income Inequality Affect Health? Health is often thought of as a product of personal...
By Leonard Pokrovski 2026-06-27 18:06:47 0 4K
Life Issues
Up. (2009)
78-year-old Carl Fredricksen travels to Paradise Falls in his house equipped with balloons,...
By Leonard Pokrovski 2023-01-27 15:43:59 0 26K
School Time
Nurturing Minds: The Significance of School Time in Children and Teenagers
Nurturing Minds: The Significance of School Time in Children and Teenagers Introduction: School...
By Leonard Pokrovski 2024-05-01 20:27:29 0 23K
Economics
What Is an Economic Expansion?
What Is an Economic Expansion? An economic expansion is a phase of the business cycle during...
By Leonard Pokrovski 2026-03-31 07:54:17 0 3K
Decision Making and Problem Solving
What are the different types of decisions?
The Geometry of the Forked Path We imagine the architecture of choice as a rigid structure....
By Michael Pokrovski 2026-06-27 14:44:05 0 614

BigMoney.VIP Powered by Hosting Pokrov