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
Personal Finance
What Are My Biggest Spending Categories?
What Are My Biggest Spending Categories? Understanding where your money goes each month is one...
By Leonard Pokrovski 2025-11-13 17:29:26 0 3K
Officiating
Sports Referring
Sports refereeing, direct management of sports competitions according to the established...
By FWhoop Xelqua 2022-12-23 15:53:19 0 21K
Business
How Much Time Does It Take to Work With Analytics Tools?
Analytics tools have become an essential part of modern business decision-making. Whether...
By Dacey Rankins 2025-08-28 17:48:52 0 7K
Transportation and Logistics
Understanding Transportation and Logistics: The Backbone of Global Trade
Transportation and logistics are two closely intertwined sectors that are essential for the...
By Dacey Rankins 2024-11-25 14:21:22 0 12K
Life Issues
Wildfire (2021)
The story of two sisters who grew up on the fractious Irish border. When one of them, who has...
By Leonard Pokrovski 2022-09-04 21:11:37 0 31K

BigMoney.VIP Powered by Hosting Pokrov