Python Dictionary Key/Value Swap

0
11K

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
История
Нюрнбергский процесс. Judgment at Nuremberg. (1961)
В Нюрнберге идет процесс над нацистскими преступниками. Слушаются дела юристов, служивших...
By Nikolai Pokryshkin 2023-02-09 12:22:42 0 26K
Business
What Are Examples of Constructive Feedback?
Constructive feedback is essential in any growth-focused environment. It helps individuals...
By Dacey Rankins 2025-07-23 12:21:36 0 4K
Social Issues
Casino. (1995)
A tale of greed, deception, money, power, and murder occur between two best friends: a mafia...
By Leonard Pokrovski 2023-02-11 19:54:34 0 24K
Социальные проблемы
Рай и ад. High and Low. (1963)
Преуспевающий бизнесмен должен решить для себя, заплатит ли он выкуп похитителям, которые по...
By Nikolai Pokryshkin 2023-01-08 22:19:53 0 21K
Business
Why Do Organizations Implement Mentoring Programs?
In today’s dynamic workplace, companies are constantly looking for ways to strengthen...
By Dacey Rankins 2025-07-09 14:36:47 0 5K

BigMoney.VIP Powered by Hosting Pokrov