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
Programming
Python Module
If you quit from the Python interpreter and enter it again, the definitions you have made...
By Jesse Thomas 2023-04-05 00:07:17 0 13K
Business
What is Product Marketing?
In today’s competitive business landscape, products alone are not enough to guarantee...
By Dacey Rankins 2025-09-13 19:08:51 0 5K
Business and Corporate Finance
What Is Corporate Finance in a Small Business?
What Is Corporate Finance in a Small Business? Applying Corporate Finance Principles at a...
By Leonard Pokrovski 2026-01-22 20:06:36 0 7K
Financial Services
Your guide to key tax terms
1. Gross and taxable income Gross income is your total income over the course of the year....
By Mark Lorenzo 2023-05-31 19:27:37 0 13K
Жизненные вопросы
Пролетая над гнездом кукушки. One flew over the cuckoo's nest. (1975)
Рэндла Патрика Макмёрфи, патологического преступника и бунтаря, переводят из колонии в...
By Nikolai Pokryshkin 2022-11-13 18:30:48 0 32K

BigMoney.VIP Powered by Hosting Pokrov