Python Dictionary Key/Value Swap

0
10K

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'}
Search
Categories
Read More
Science
Unveiling the Wonders of Science: A Journey into Discovery
Science, the systematic pursuit of knowledge through observation, experimentation, and...
By Dacey Rankins 2024-05-22 14:31:31 0 12K
Video
The Best Branded Videos of All Time, Helpful Tips for Inspiration
Creating effective branded videos is a long-term process that requires constant creativity and...
By FWhoop Xelqua 2023-06-10 20:07:43 0 27K
Социальные проблемы
Зелёная миля. The Green Mile. (1999)
Пол Эджкомб — начальник блока смертников в тюрьме «Холодная гора», каждый из...
By Nikolai Pokryshkin 2022-11-20 23:28:07 0 25K
Библиотеки
Библиотека бизнесмена.
Государство и право. Мир «Игры престолов» — это мир подлости, разврата и...
By Alexander Petrov 2023-01-20 23:50:16 0 24K
Business
What Are the Company's Core Values and Mission?
Every successful organization is built on a foundation of purpose and guiding principles. A...
By Dacey Rankins 2025-08-05 15:08:35 0 2K

BigMoney.VIP Powered by Hosting Pokrov



Top sites Pokrov