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
Business
How Does E-Commerce Work?
A customer taps a screen at 11:43 p.m. Three days later, a package appears on a front porch...
By Dacey Rankins 2026-05-13 14:19:43 0 3K
Business
How Do I Create an On-Demand App?
The first time I watched a founder explain their idea for an on-demand app, the presentation...
By Dacey Rankins 2026-06-30 21:39:43 0 2K
Other
David Attenborough: A Life on Our Planet. (2020)
One man has seen more of the natural world than any other. This unique feature documentary is his...
By Leonard Pokrovski 2023-07-02 19:41:03 0 39K
Научная фантастика и фэнтези
Чёрная вдова. Black Widow. (2021)
Наташе Романофф предстоит лицом к лицу встретиться со своим прошлым. Чёрной Вдове придется...
By Nikolai Pokryshkin 2022-09-11 22:38:36 0 25K
Business
How Does Franchising Work?
The first time I sat inside a franchise owner’s office, I expected entrepreneurial chaos....
By Dacey Rankins 2026-05-29 16:59:04 0 5K

BigMoney.VIP Powered by Hosting Pokrov