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
History
Paths of Glory. (1957)
After refusing to attack an enemy position, a general accuses the soldiers of cowardice and their...
By Leonard Pokrovski 2022-12-17 19:54:32 0 27K
Business
Can Franchise Owners Make Independent Decisions?
The answer most prospective franchise buyers want is simple. Yes or no. Freedom or restriction....
By Dacey Rankins 2026-06-02 20:08:41 0 3K
Organizations
WHO: what is it, decoding, history of creation
WHO: what is it, decoding, history of creation World Health Day is celebrated annually...
By Leonard Pokrovski 2024-04-20 21:42:38 0 18K
Economics
What Are the Types of Economic Systems?
What Are the Types of Economic Systems?Capitalism, Socialism, Communism, and Mixed Economies An...
By Leonard Pokrovski 2026-01-27 18:54:59 0 6K
Business
How Do You Prioritize Features in Planning?
Feature prioritization is one of the most critical and challenging responsibilities in product...
By Dacey Rankins 2025-08-20 15:54:27 0 9K

BigMoney.VIP Powered by Hosting Pokrov