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
Retail Trade
Retail
Retail trade is the business activity of selling goods or providing services directly to end...
By Dacey Rankins 2024-03-18 17:20:11 0 27K
Социальные проблемы
Славные парни. Goodfellas. (1990)
История о Генри Хилле — начинающем гангстере, занимающемся грабежами вместе с подельниками...
By Nikolai Pokryshkin 2022-11-13 09:07:34 0 34K
Walking
What is the difference between race walking and running and from ordinary walking
This is walking - but the technique of its performance is far from classical walking. It's not a...
By FWhoop Xelqua 2022-11-04 19:06:30 0 23K
Productivity
What is collaborative task management?
What Is Collaborative Task Management? In today’s fast-paced, interconnected work...
By Michael Pokrovski 2026-03-21 20:26:36 0 2K
Personal Finance
What Percentage of My Salary Should Go to Savings? Budgeting Frameworks Explained
What Percentage of My Salary Should Go to Savings? Budgeting Frameworks Explained Figuring out...
By Leonard Pokrovski 2025-12-05 19:43:56 0 4K

BigMoney.VIP Powered by Hosting Pokrov