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'}
Zoeken
Categorieën
Read More
Personal Finance
How to Manage Day-to-Day Spending vs Saving vs Investing
How to Manage Day-to-Day Spending vs Saving vs Investing Balancing the “Now” with...
By Leonard Pokrovski 2025-11-10 18:36:45 0 1K
Business
What Are the Key Responsibilities of a Startup CEO?
  The role of a startup CEO is both exhilarating and daunting. Unlike in mature companies...
By Dacey Rankins 2025-05-01 16:21:54 0 8K
Life Issues
Wicked Minds. (2003)
A young man returns home from college to find his father remarried, until the sudden murder of...
By Leonard Pokrovski 2023-05-04 18:28:23 0 28K
Marketing and Advertising
How Do I Measure the Success of Social Media Marketing (ROI)?
Measuring success in social media marketing is one of the biggest challenges for businesses of...
By Dacey Rankins 2025-11-03 15:00:19 0 3K
Business
B2C: how the relationship between business and buyer works
Features of B2C salesA business that is aimed at an ordinary buyer has its own characteristics....
By Dacey Rankins 2024-09-02 19:00:28 0 13K

BigMoney.VIP Powered by Hosting Pokrov