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
Social Issues
Capturing the Friedmans (2003)
Documentary on the Friedmans, a seemingly typical, upper-middle-class Jewish family whose world...
By Leonard Pokrovski 2023-07-31 20:28:12 0 537K
Business
What are must-have desk accessories?
The desk looked perfect in photos. Soft lighting. Minimal objects. A single laptop centered like...
By Dacey Rankins 2026-05-07 01:04:52 0 4K
Economics
What is a carbon tax?
What Is a Carbon Tax? A refinery in Texas exhales more carbon dioxide before dawn than a forest...
By Leonard Pokrovski 2026-05-22 21:27:42 0 4K
Personal Finance
What If I Overspend?
What If I Overspend? How to Regain Control and Restore Balance to Your Budget Everyone has been...
By Leonard Pokrovski 2025-10-22 12:42:43 0 4K
Financial Services
Public Service Loan Forgiveness: a path out of student debt
Do you work for a government or nonprofit organization? Or are you considering that career...
By Mark Lorenzo 2023-06-02 20:12:52 0 22K

BigMoney.VIP Powered by Hosting Pokrov