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'}
Search
Categories
Read More
Economics
Why do currencies rise and fall?
Currencies are often described as “strong” or “weak,” but those labels...
By Leonard Pokrovski 2026-04-23 17:21:40 0 2K
Animal
Animal Health: Key Aspects and Tips for Ensuring Well-being
Animal health is essential not only for the welfare of individual animals but also for the health...
By Dacey Rankins 2024-11-21 15:55:46 0 16K
Социальные проблемы
Квартира. The Apartment. (1960)
Скромный служащий страховой компании Бакстер, чтобы хоть как-то продвинуться по карьерной...
By Nikolai Pokryshkin 2023-01-18 22:38:50 0 34K
Economics
Is Comparative Economics a Good Subject to Study?
Is Comparative Economics a Good Subject to Study? Choosing a field of study is one of the most...
By Leonard Pokrovski 2026-03-06 20:35:47 0 10K
Personal Finance
What Happens If I Can’t Pay My Student Loans?
What Happens If I Can’t Pay My Student Loans? Deferment, Forbearance, Default, and...
By Leonard Pokrovski 2025-12-17 16:02:41 0 5K

BigMoney.VIP Powered by Hosting Pokrov