Python Dictionary Key/Value Swap

0
13K

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
Economics
The Role of Technology in Economic Development
The Role of Technology in Economic Development Technology plays a central and transformative...
By Leonard Pokrovski 2026-04-11 00:42:29 0 1K
Business
What Are the Main Responsibilities of a CEO?
The Chief Executive Officer (CEO) is the highest-ranking executive in a company, responsible for...
By Dacey Rankins 2025-06-13 17:36:25 0 11K
Television
UN press briefings. Live TV. USA.
The UN Web TV Channel is available 24 hours a day with selected live programming of United...
By Nikolai Pokryshkin 2022-10-12 19:01:24 0 31K
Business and Society
10 Tips for a New Entrepreneur
An entrepreneur differs from the rest not only in his activity, but also in his consciousness,...
By Nikolai Pokryshkin 2022-08-03 21:24:24 0 52K
Mental Health
The Influence of Competition on Human Behavior and Social Interactions
Competition is a powerful force that shapes human behavior and social interactions in profound...
By Dacey Rankins 2025-01-14 14:52:07 0 17K

BigMoney.VIP Powered by Hosting Pokrov