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
Marketing and Advertising
Is Radio Advertising Good for Small Businesses?
For small businesses, every marketing dollar matters. Limited budgets, local competition, and the...
By Dacey Rankins 2026-02-09 16:34:04 0 3K
Finance
How Long Does It Take to Read Finance Biographies?
How Long Does It Take to Read Finance Biographies? A Look at Why They’re So Dense —...
By Leonard Pokrovski 2025-10-08 20:59:04 0 12K
Social Issues
North by Northwest. (1959)
A New York City advertising executive goes on the run after being mistaken for a government agent...
By Leonard Pokrovski 2023-01-17 19:04:53 0 26K
Business
What Is a CMMS (Computerized Maintenance Management System)?
A Computerized Maintenance Management System (CMMS) is one of the most important digital tools...
By Dacey Rankins 2026-04-16 15:33:01 0 2K
Financial Services
Law of supply
Key points The law of supply states that a higher price leads to a higher...
By Mark Lorenzo 2023-06-28 19:25:37 0 17K

BigMoney.VIP Powered by Hosting Pokrov