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
How does inflation affect savings?
How Does Inflation Affect Savings? Saving money is one of the most important habits for...
By Leonard Pokrovski 2026-07-23 03:20:08 0 3K
Economics
When do credit cards charge interest?
When Do Credit Cards Charge Interest? Credit cards are convenient financial tools, but...
By Leonard Pokrovski 2026-08-04 19:11:39 0 3K
Социальные проблемы
Авантюристки. Burraco fatale. (2020)
Евгения — домохозяйка, во всем покорная мужу. Рина — клептоманка и патологическая...
By Nikolai Pokryshkin 2022-10-09 17:52:52 0 41K
Economics
What causes an economic crisis?
What Causes an Economic Crisis? There is a peculiar ritual modern societies perform every decade...
By Leonard Pokrovski 2026-05-12 17:59:24 0 6K
Nature and Science
Grizzly Man (2005)
A devastating and heart-rending take on grizzly bear activists Timothy Treadwell and Amie...
By Leonard Pokrovski 2023-07-28 20:39:14 0 526K

BigMoney.VIP Powered by Hosting Pokrov