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
Productivity
How to stop bad habits?
How to Stop Bad Habits? Most people try to stop bad habits by applying pressure. They rely on...
By Michael Pokrovski 2026-05-21 18:52:01 0 1K
Business
What Skills and Qualities Make a Successful CEO?
Being a Chief Executive Officer (CEO) is about much more than holding the top title in an...
By Dacey Rankins 2025-08-06 10:58:32 0 6K
Economics
Does income inequality cause crime?
Does Income Inequality Cause Crime? Few social issues generate as much debate as the...
By Leonard Pokrovski 2026-06-27 18:07:13 0 2K
Personal Finance
What Is SSI and How Is It Different From SSDI?
What Is SSI and How Is It Different From SSDI? When people talk about disability benefits in the...
By Leonard Pokrovski 2025-12-11 20:05:22 0 8K
Социальные проблемы
Реквием по мечте. Requiem for a Dream. (2000)
Каждый стремится к своей заветной мечте. Сара Голдфарб мечтала сняться в известном телешоу, ее...
By Nikolai Pokryshkin 2023-01-06 21:31:16 0 29K

BigMoney.VIP Powered by Hosting Pokrov