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
Спорт
Сестры на старте. Sisters on Track. (2021)
Документальный фильм Netflix "Sisters On Track" - это дико аутентичная история взросления трех...
By Nikolai Pokryshkin 2022-09-03 20:46:51 0 35K
Philosophy
What is philosophy?
Life, science and culture in general require from us and, above all, from young people,...
By FWhoop Xelqua 2023-01-12 15:20:55 0 19K
Textiles and Nonwovens
What are nonwoven materials: types and applications
What is non-woven material Non-woven fabrics have properties and characteristics that are...
By Michael Pokrovski 2024-03-19 21:27:54 0 33K
Bedrijvengids
The Treasure of the Sierra Madre. (1948)
Two down-on-their-luck Americans searching for work in 1920s Mexico convince an old prospector to...
By Leonard Pokrovski 2023-02-20 17:13:40 0 21K
Personal Finance
How Does My Spending Align With My Values and Life Goals?
How Does My Spending Align With My Values and Life Goals? Money is more than a means of exchange...
By Leonard Pokrovski 2025-11-13 17:28:22 0 1K

BigMoney.VIP Powered by Hosting Pokrov