Python Dictionary Key/Value Swap

0
11K

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
The structure of costs in the short run
Key points Looked at from a short-run perspective, a firm’s total costs can...
By Mark Lorenzo 2023-02-08 12:03:24 0 11K
Business
How Should Edge Cases Be Handled in User Stories?
When writing user stories in Agile development, most teams naturally focus on the happy...
By Dacey Rankins 2025-08-26 11:46:24 0 2K
Business
How Can I Prevent Conflicts from Escalating?
Prevent escalation by staying calm, practicing active listening, using "I" statements, and...
By Dacey Rankins 2025-06-04 14:21:29 0 5K
Programming
Discord.py vs Nextcord
When making a bot on Discord, you've probably ran into 2 libraries that looks really similar. In...
By Jesse Thomas 2023-02-07 21:56:18 0 10K
Business
How to Become a CEO: A Step-by-Step Guide
Becoming a CEO is the pinnacle of success for many professionals in the business world....
By Dacey Rankins 2025-06-17 15:59:24 0 6K

BigMoney.VIP Powered by Hosting Pokrov