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
Personal Finance
What Is a Good Credit Score? What Number Counts as “Good” or “Excellent” Credit?
What Is a Good Credit Score? What Number Counts as “Good” or “Excellent”...
By Leonard Pokrovski 2025-11-17 17:57:13 0 1K
Business
Product marketing: what is it and why is it needed in IT  
Product marketing: what is it and why is it needed in IT   Launching a new product on...
By Leonard Pokrovski 2024-08-28 21:41:43 0 22K
Entertainment
The Best Free Entertainment in the World
It turns out that even in the most expensive cities in the world there are many ways to spend...
By FWhoop Xelqua 2022-10-20 12:27:56 0 27K
Business
How Can I Write a Compelling Biography of a Leader?
A leader’s biography should capture their achievements, challenges, and personality while...
By Dacey Rankins 2025-08-13 20:14:30 0 5K
Business
What is the Difference Between Business Strategy and Business Plan?
When starting or managing a business, it's crucial to understand the distinction between a...
By Dacey Rankins 2024-12-24 13:12:02 0 10K

BigMoney.VIP Powered by Hosting Pokrov