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
Issues
Global problems of mankind
Progress moves humanity forward, but it also has a downside – global problems. In this...
By FWhoop Xelqua 2023-01-31 15:52:21 0 23K
Marketing and Advertising
How Long Should a Marketing Plan Be?
Introduction One of the most common questions marketers face when designing a marketing plan is:...
By Dacey Rankins 2025-10-15 18:53:04 0 6K
Business
How Do I Give Effective Feedback?
Giving feedback is one of the most powerful tools a leader, mentor, or peer can use to support...
By Dacey Rankins 2025-07-23 12:15:28 0 6K
Economics
How do developing countries promote economic development?
Developing countries face a complex set of challenges in their pursuit of economic development,...
By Leonard Pokrovski 2026-04-11 17:46:32 0 4K
Horror
The Silence of the Lambs. (1991)
A young F.B.I. cadet must receive the help of an incarcerated and manipulative cannibal killer to...
By Leonard Pokrovski 2022-11-18 19:22:21 0 23K

BigMoney.VIP Powered by Hosting Pokrov