Python Dictionary Key/Value Swap

0
14K

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'}
Căutare
Categorii
Citeste mai mult
Human Resources
How Does Offshoring Affect Globalization?
Offshoring and globalization are deeply interconnected concepts that have reshaped the modern...
By Dacey Rankins 2026-03-31 18:02:32 0 6K
Economics
What is pollution in economic terms?
What Is Pollution in Economic Terms? There is a habit in modern economics of describing the...
By Leonard Pokrovski 2026-05-21 19:27:33 0 4K
Economics
What is the difference between headline and core inflation?
What Is the Difference Between Headline and Core Inflation? Inflation is one of the most closely...
By Leonard Pokrovski 2026-07-22 05:10:44 0 2K
Decision Making and Problem Solving
How to improve long-term memory?
The mind is not a warehouse, and it is certainly not a hard drive. We operate under the...
By Michael Pokrovski 2026-07-17 23:57:51 0 1K
Жизненные вопросы
Три билборда на границе Эббинга, Миссури. Three Billboards Outside Ebbing, Missouri. (2017)
Спустя несколько месяцев после убийства дочери Милдред Хейс преступники так и не найдены....
By Nikolai Pokryshkin 2023-03-07 23:38:28 0 31K

BigMoney.VIP Powered by Hosting Pokrov