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'}
Search
Categories
Read More
Business
How Do I Handle Open‑Ended Responses?
Open-ended responses are a goldmine of insight—rich with context, emotion, and nuance that...
By Dacey Rankins 2025-07-28 14:56:02 0 4K
Brokerage and Mortgaging
In the US, the average mortgage rate for the first time since 2008 exceeded 6%
The number of applications for mortgage loans fell by 64% compared to last year. In the US, the...
By FWhoop Xelqua 2022-10-12 13:09:00 0 25K
Ужасы
Нечестивые. The Unholy. (2021)
На дереве, где 150 лет назад была повешена и сожжена обвинённая в колдовстве, глухонемая...
By Nikolai Pokryshkin 2022-09-21 10:56:12 0 36K
Programming
Remove Keys from Dictionary - Python
In order to delete keys from the diction, you would have to use 'del'. Here's an example:x ...
By Jesse Thomas 2023-02-23 23:16:41 0 11K
Wholesale Trade
Understanding Wholesale Trade: The Heart of the Supply Chain
Wholesale trade is a critical component of the global economy, acting as a middleman between...
By Dacey Rankins 2025-01-21 14:34:30 0 15K

BigMoney.VIP Powered by Hosting Pokrov