Python Dictionary Key/Value Swap

0
5K

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
Programming
JavaScript Random Function
The Math.random() static method returns a floating-point, pseudo-random number that's...
By Jesse Thomas 2023-06-02 21:17:49 0 4K
Arts, Culture and Entertainment
Whiplash. (2014)
A promising young drummer enrolls at a cut-throat music conservatory where his dreams of...
By Leonard Pokrovski 2022-12-04 20:07:30 0 14K
Food
Best Foods for Weight Loss
Studies have shown that certain foods can affect appetite, so they may be beneficial for weight...
By FWhoop Xelqua 2022-09-21 14:00:25 0 19K
Business
Automation of the company's business processes
Automation of the company's business processesAutomation of business processes helps to save...
By Dacey Rankins 2024-09-23 16:34:20 0 10K
Business
Strategic Planning in Business: Stages and Benefits
Strategic Planning in Business: Stages and Benefits The article explains what strategic...
By Leonard Pokrovski 2024-07-16 17:47:43 0 10K
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov