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
Human Resources
How Does Personnel Management Motivate Employees?
Employee motivation is one of the most important responsibilities of personnel management....
By Dacey Rankins 2026-04-10 20:10:37 0 4K
Economics
What are the major global markets?
What Are the Major Global Markets? The World’s Economic Engine Room Is Bigger Than Most...
By Leonard Pokrovski 2026-06-04 23:35:16 0 7K
Personal Finance
Moody's downgrades Israel's credit rating
The international rating agency Moody's has downgraded Israel's credit rating by two notches at...
By Dacey Rankins 2024-10-23 16:20:52 0 23K
Business
Can I Use Images From the Internet Legally?
The image was perfect. It captured exactly the mood the company wanted. The colors worked. The...
By Dacey Rankins 2026-06-11 12:31:42 0 4K
Financial Services
The Production Possibilities Frontier
Key Points The Production Possibilities Frontier (PPF) is a graph that shows all the...
By Mark Lorenzo 2023-04-05 17:09:37 0 14K

BigMoney.VIP Powered by Hosting Pokrov