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
Financial Services
Indexing and its limitations
Key points A payment is said to be indexed if it is automatically...
By Mark Lorenzo 2023-03-24 19:47:37 0 15K
Economics
Can free enterprise lead to inequality?
Can Free Enterprise Lead to Inequality? The Question Nobody Wants to Answer Honestly Walk into...
By Leonard Pokrovski 2026-06-06 17:02:35 0 13K
Business
What Are the Applications of Management Science?
Management Science is a powerful tool that finds practical application across a wide range of...
By Dacey Rankins 2025-07-02 15:09:00 0 9K
People
50 artists that left a mark in history
50 artists and their works that have left a mark on history and people's hearts...
By FWhoop Xelqua 2023-07-09 18:10:51 0 33K
Economics
How does inflation affect retirement?
How Does Inflation Affect Retirement? Retirement is often viewed as a time to relax and enjoy...
By Leonard Pokrovski 2026-07-23 03:16:57 0 3K

BigMoney.VIP Powered by Hosting Pokrov