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'}
Search
Categories
Read More
История
Нюрнбергский процесс. Judgment at Nuremberg. (1961)
В Нюрнберге идет процесс над нацистскими преступниками. Слушаются дела юристов, служивших...
By Nikolai Pokryshkin 2023-02-09 12:22:42 0 27K
Business
How Do You Prioritize Tasks and Manage Dependencies? Critical Path, Sequencing, and Reprioritization Strategies
In any project, time is limited and tasks are interconnected. Knowing what to do first—and...
By Dacey Rankins 2025-07-14 17:05:41 0 6K
Социальные проблемы
Плохие сказки. Bad Tales. (2020)
Деннис и Алессия по просьбе отца показывают свои дневники с отличными оценками многочисленным...
By Nikolai Pokryshkin 2022-10-19 16:49:19 0 25K
Mental Health
Autism Spectrum: Genes
Autism has a strong genetic basis, although the genetics of autism are complex and it is unclear...
By Kelsey Rodriguez 2023-02-21 13:32:51 0 14K
Marketing and Advertising
How Should a Marketing Plan Vary for Different Contexts?
Introduction Marketing plans are never one-size-fits-all. Every organization — whether...
By Dacey Rankins 2025-10-15 18:16:23 0 2K

BigMoney.VIP Powered by Hosting Pokrov