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
What Is SaaS (Software as a Service)?
Most people use SaaS every day without realizing they are participating in one of the most...
By Dacey Rankins 2026-05-13 12:17:02 0 2K
Central America
Central America Murders
On January 11, 2017, a major event occurred in El Salvador: no one was killed. The news of a day...
By FWhoop Xelqua 2023-04-12 18:10:29 0 26K
News and Media
15 fascinating popular science channels
1. AsapSCIENCE Did the egg come before the chicken or vice versa? The authors of the...
By FWhoop Xelqua 2023-04-05 13:31:53 0 31K
Personal Finance
What Are the Common Tax Credits I Might Qualify For?
What Are the Common Tax Credits I Might Qualify For? How Tax Credits Reduce Your Tax Bill and...
By Leonard Pokrovski 2025-11-21 19:44:25 0 8K
Human Resources
What Are the Objectives of Personnel Management?
Personnel management is a vital organizational function that focuses on managing employees...
By Dacey Rankins 2026-04-09 18:24:20 0 2K

BigMoney.VIP Powered by Hosting Pokrov