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'}
Zoeken
Categorieën
Read More
Parallel Computing
Scientists have found a way to double the speed of computer calculations without replacing hardware
At the 56th Annual IEEE/ACM International Symposium on Microarchitecture, researchers from the...
By Michael Pokrovski 2024-03-22 20:43:29 0 25K
Advice
Secrets of Productivity
Is this productivity needed at all?Productive – creating value, bringing results. The...
By FWhoop Xelqua 2023-06-05 19:00:41 0 20K
Financial Services
Changes in equilibrium price and quantity: the four-step process
Key points There is a four-step process that allows us to predict how an event...
By Mark Lorenzo 2023-04-10 20:19:42 0 21K
Activism
Human rights education and activism
What is activism? The word "activism" is only about 100 years old, at least in its current...
By FWhoop Xelqua 2022-11-19 14:01:26 0 27K
Business
Are Contributions to Crowdfunding Campaigns Tax-Deductible?
Crowdfunding has become a popular way for individuals and organizations to raise money for a wide...
By Dacey Rankins 2025-03-28 14:35:19 0 11K

BigMoney.VIP Powered by Hosting Pokrov