Python Dictionary Key/Value Swap

0
9كيلو بايت

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'}
البحث
الأقسام
إقرأ المزيد
Социальные проблемы
Аферистка. I Care a Lot. (2021)
Марла руководствуется принципом «побеждает сильнейший». Женщина организовала...
بواسطة Nikolai Pokryshkin 2022-09-15 20:38:31 0 30كيلو بايت
Television
WTNH News 8 Newscasts Live TV. New Haven, Connecticut, USA.
WTNH, channel 8, is an ABC-affiliated television station located in New Haven, Connecticut, USA....
بواسطة Nikolai Pokryshkin 2022-11-08 19:34:30 0 23كيلو بايت
Television
Parliament On Demand, Live TV, New Zealand.
Welcome to Parliament on Demand, the home of Parliament TV and all of New Zealand Parliament's...
بواسطة Nikolai Pokryshkin 2022-11-11 19:47:53 0 22كيلو بايت
Biology
The Role of Competition in Natural Selection and Evolution
Competition is a driving force in the natural world, shaping the survival and evolution of...
بواسطة Dacey Rankins 2025-01-14 14:46:24 0 7كيلو بايت
Arts, Culture and Entertainment
The Prom (2020)
In desperate need of a noble cause to revive their public images and bounce back, self-centred...
بواسطة Leonard Pokrovski 2022-09-26 20:48:09 0 22كيلو بايت
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov