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'}
Buscar
Categorías
Read More
Business
What to do if you have a mobile app idea: a beginner's guide.
What to do if you have a mobile app idea: a beginner's guide. One fine morning, you wake up with...
By Leonard Pokrovski 2024-08-28 22:25:54 0 22K
Programming
PEP8 Quick Tip
When indenting, use tabs instead of spaces. Yes, PEP8 does tell you that spaces are suggested for...
By Jesse Thomas 2023-02-09 22:47:21 0 11K
Business
Product Development: In-House vs. Outsourcing
Product Development: In-House vs. OutsourcingThere are two approaches to developing digital...
By Dacey Rankins 2024-09-20 18:50:24 0 16K
Drugs
Exploring the Recreational Use of Prescription Drugs: Balancing Benefits and Risks
Exploring the Recreational Use of Prescription Drugs: Balancing Benefits and Risks Introduction:...
By Leonard Pokrovski 2024-05-09 10:00:34 0 19K
Cooking
Only chefs know them: 9 simple tips to make food healthier
If you are thinking about how useful the food that you put on your plate at home is, then you...
By FWhoop Xelqua 2023-05-08 19:24:34 0 22K

BigMoney.VIP Powered by Hosting Pokrov