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'}
Pesquisar
Categorias
Leia Mais
Business
How Do I Measure Progress in a Lean Startup?
In a Lean Startup, progress isn’t measured by traditional business metrics like total...
Por Dacey Rankins 2025-03-10 17:30:00 0 11K
Life Issues
Aladdin (1992)
A kind-hearted street urchin and a power-hungry Grand Vizier vie for a magic lamp that has the...
Por Leonard Pokrovski 2023-07-01 19:45:10 0 33K
Социальные проблемы
Расёмон. Rashomon. (1950)
Древняя Япония. В лесу изнасилована женщина, а её муж убит. Есть четыре точки зрения на это...
Por Nikolai Pokryshkin 2023-02-25 13:44:25 0 19K
Television
UN press briefings. Live TV. USA.
The UN Web TV Channel is available 24 hours a day with selected live programming of United...
Por Nikolai Pokryshkin 2022-10-12 19:01:24 0 27K
Mental Health
Autism Spectrum: Diagnosis Pt. 2
Ideally the diagnosis of ASD should be given by a team of clinicians (e.g. pediatricians, child...
Por Kelsey Rodriguez 2023-03-03 18:22:53 0 12K

BigMoney.VIP Powered by Hosting Pokrov