Python Dictionary Key/Value Swap

0
12K

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
Жизненные вопросы
Седьмая Печать. The Seventh Seal. (1957)
В середине XIV века рыцарь Антониус Блок и его оруженосец возвращаются после десяти лет крестовых...
By Nikolai Pokryshkin 2023-04-07 13:20:35 0 31K
Business
Business Automation in 2024: Top Five Trends
Business Automation in 2024: Top Five Trends Business automation is often...
By Leonard Pokrovski 2024-08-11 16:57:31 0 20K
Business
What Is One Decision You Regret, and What Did You Learn From It?
Every leader has faced moments where hindsight brings clarity. Regret is not a sign of...
By Dacey Rankins 2025-05-09 15:06:22 0 7K
Социальные проблемы
Американская история Икс. American History X (1998)
Лидер местной банды скинхедов Дерек Виньярд прочно удерживает авторитет в своём районе....
By Nikolai Pokryshkin 2022-11-29 10:39:05 0 43K
Mental Health
ADHD: Brain structure
 The left prefrontal cortex, shown here in blue, is often affected in ADHD.In children with...
By Kelsey Rodriguez 2023-04-11 17:03:06 0 12K

BigMoney.VIP Powered by Hosting Pokrov