Python Dictionary Key/Value Swap

0
13χλμ.

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'}
Αναζήτηση
Κατηγορίες
Διαβάζω περισσότερα
История
Унесённые ветром. Gone with the Wind. (1939)
Могучие ветры Гражданской войны в один миг уносят беззаботную юность южанки Скарлетт О`Хара,...
από Nikolai Pokryshkin 2023-03-03 15:06:58 0 21χλμ.
Programming
Python Module
If you quit from the Python interpreter and enter it again, the definitions you have made...
από Jesse Thomas 2023-04-05 00:07:17 0 12χλμ.
Business
Inspirational Checklist: 25 Things to Try If You Want to Change Your Life
  The list was compiled by psychologists, coaches and personal growth...
από Leonard Pokrovski 2024-07-16 17:38:34 0 19χλμ.
Marketing and Advertising
How Many People See a Billboard Each Day?
One of the most common questions advertisers ask when considering billboard advertising is simple...
από Dacey Rankins 2026-01-14 14:02:19 0 1χλμ.
Mental Health
Understanding Mental Health: Importance, Challenges, and Ways to Improve Well-Being
Mental health is an integral part of overall well-being, influencing how we think, feel, and act....
από Dacey Rankins 2024-11-22 14:53:42 0 13χλμ.

BigMoney.VIP Powered by Hosting Pokrov