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
Personal Finance
What Happens to Social Security When You Die?
What Happens to Social Security When You Die? Survivor Benefits, Death Benefits, and Support for...
By Leonard Pokrovski 2025-12-10 22:47:43 0 6K
Productivity
How do I create a time management plan?
Time is a finite resource. Whether you are a student, a professional, an entrepreneur, or...
By Michael Pokrovski 2026-03-05 17:42:53 0 24K
Science Fiction and Fantasy
Harry Potter and the Deathly Hallows: Part 2. (2011)
Harry, Ron, and Hermione search for Voldemort's remaining Horcruxes in their effort to destroy...
By Leonard Pokrovski 2023-03-21 20:12:55 0 30K
Business
Can a License Be Revoked?
Most people think about licenses as permissions. Permission to use software. Permission to sell...
By Dacey Rankins 2026-06-13 12:36:00 0 1K
Economics
What Is Panel Data in Econometrics?
What Is Panel Data in Econometrics? Panel data, also known as longitudinal data, is one of the...
By Leonard Pokrovski 2026-03-20 02:54:44 0 3K

BigMoney.VIP Powered by Hosting Pokrov