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
Human Resources
How Much Does Outsourcing Cost?
Outsourcing is often associated with cost savings, but the reality is more nuanced. While...
By Dacey Rankins 2026-04-06 18:33:36 0 3K
Социальные проблемы
Большой куш. Snatch. (2000)
Фрэнки Четыре Пальца должен был переправить краденый алмаз из Англии в США своему боссу Эви, но,...
By Nikolai Pokryshkin 2023-01-31 12:31:53 0 45K
Economics
How much government intervention is too much?
How Much Government Intervention Is Too Much? The Question Nobody Wants to Answer Honestly Ask...
By Leonard Pokrovski 2026-06-06 17:05:53 0 4K
Business
Who Can Benefit from Mentoring?
Mentoring is often associated with early-career professionals, but its value extends far beyond...
By Dacey Rankins 2025-07-18 17:35:49 0 5K
Business
How Much Can a Membership Site Earn?
It is usually the first question entrepreneurs ask. Not, "What problem should I solve?" Not,...
By Dacey Rankins 2026-06-26 20:12:28 0 1K

BigMoney.VIP Powered by Hosting Pokrov