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'}
Pesquisar
Categorias
Leia Mais
Business
Employee Motivation: Trends, Science, Practice, Assessment
Employee Motivation: Trends, Science, Practice, AssessmentMaterial and Non-Material Incentives...
Por Leonard Pokrovski 2024-08-11 16:05:13 0 21K
Personal Finance
What If I Overspend?
What If I Overspend? How to Regain Control and Restore Balance to Your Budget Everyone has been...
Por Leonard Pokrovski 2025-10-22 12:42:43 0 2K
Mental Health
Dementia: Prodromal
Pre-dementia states considered as prodromal are mild cognitive impairment (MCI), and mild...
Por Kelsey Rodriguez 2023-07-18 18:47:51 0 14K
Жизненные вопросы
Начало. Inception. (2010)
Кобб – талантливый вор, лучший из лучших в опасном искусстве извлечения: он крадет ценные...
Por Nikolai Pokryshkin 2022-11-11 18:10:34 0 31K
История
Пожары. Incendies. (2010)
Когда брат и сестра Марван ознакомились с завещанием матери, их удивлению не было предела. Её...
Por Nikolai Pokryshkin 2023-01-23 12:00:36 0 33K

BigMoney.VIP Powered by Hosting Pokrov