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
Business and Corporate Finance
How Do Business Loans Work?
How Do Business Loans Work? Understanding Interest Rates, Repayment Terms, and Eligibility...
By Leonard Pokrovski 2026-01-06 07:10:52 0 11K
Economics
How Does Brexit Affect Travel to the EU?
How Does Brexit Affect Travel to the EU? Visas, Passports, and Border Rules Explained Brexit...
By Leonard Pokrovski 2026-02-04 00:21:45 0 4K
Economics
What happened in past financial crises?
What Happened in Past Financial Crises? The Most Expensive Lessons in Economic History Every...
By Leonard Pokrovski 2026-06-11 03:55:34 0 1K
Научная фантастика и фэнтези
Темный рыцарь. The Dark Knight. (2008)
Бэтмен поднимает ставки в войне с криминалом. С помощью лейтенанта Джима Гордона и прокурора...
By Nikolai Pokryshkin 2022-10-28 21:31:58 0 37K
Economics
What Is Import Policy?
What Is Import Policy? An import policy is a set of rules and measures a government uses to...
By Leonard Pokrovski 2026-02-24 17:51:00 0 5K

BigMoney.VIP Powered by Hosting Pokrov