Python Dictionary Key/Value Swap

0
11Кб

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'}
Поиск
Категории
Больше
Социальные проблемы
Рай и ад. High and Low. (1963)
Преуспевающий бизнесмен должен решить для себя, заплатит ли он выкуп похитителям, которые по...
От Nikolai Pokryshkin 2023-01-08 22:19:53 0 21Кб
Subcultures
Exploring Society's Subcultures: Unveiling the Tapestry of Diversity
Society is a kaleidoscope of human experiences, where individuals come together, each bringing...
От Dacey Rankins 2024-06-13 16:22:46 0 17Кб
Organizations
WHO: what is it, decoding, history of creation
WHO: what is it, decoding, history of creation World Health Day is celebrated annually...
От Leonard Pokrovski 2024-04-20 21:42:38 0 15Кб
Personal Finance
What is a loan in simple words
Forms of lendingThere are many classifications of loans. For example, if you divide loans by...
От Dacey Rankins 2024-10-28 19:00:18 0 12Кб
Animation
Top 10 Greatest Anime
Anime, a wondrous and captivating form of storytelling, has its roots in the land of the rising...
От FWhoop Xelqua 2023-04-27 17:44:48 0 18Кб

BigMoney.VIP Powered by Hosting Pokrov