Python Dictionary Key/Value Swap

0
14K

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
Economics
What is economic integration?
What Is Economic Integration? Economic integration is the process by which countries reduce or...
By Leonard Pokrovski 2026-08-18 13:46:06 0 3K
Business
How Does Automation Improve Productivity?
Automation has become one of the most powerful drivers of productivity in modern workplaces. By...
By Dacey Rankins 2026-04-23 16:59:39 0 6K
Economics
What is mediation in industrial relations?
What Is Mediation in Industrial Relations? Industrial relations involve the relationship between...
By Leonard Pokrovski 2026-07-14 18:48:33 0 2K
Business
What Is the Shared Responsibility Model?
The first time I heard the phrase shared responsibility model, I thought it sounded suspiciously...
By Dacey Rankins 2026-07-29 23:08:21 0 922
История
Список Шиндлера. Schindler's List. (1993)
Сценарий «Списка Шиндлера» долго кочевал от одного режиссера к другому, пока не...
By Nikolai Pokryshkin 2022-10-31 20:54:26 0 43K

BigMoney.VIP Powered by Hosting Pokrov