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
Decision Making and Problem Solving
Creativity vs critical thinking
The Seance and the Scalpel We are trying to catch a ghost with a net made of razor blades....
By Michael Pokrovski 2026-06-26 17:25:51 0 2K
Finance
The Risks and Downsides of Fintech: Security, Privacy, Fraud, Regulatory, and Technological Risks
The Risks and Downsides of Fintech: Security, Privacy, Fraud, Regulatory, and Technological Risks...
By Leonard Pokrovski 2025-10-10 22:49:33 0 13K
Business
What Is Cloud Storage?
Most people never think about where their data lives. A photo is taken. A document is saved. A...
By Dacey Rankins 2026-06-05 19:40:17 0 2K
Business
How do I find my first customers?
Every startup starts with zero customers. The first few are the hardest to get—but...
By Dacey Rankins 2025-04-11 16:58:52 0 19K
Mental Health
ADHD: Brain structure
 The left prefrontal cortex, shown here in blue, is often affected in ADHD.In children with...
By Kelsey Rodriguez 2023-04-11 17:03:06 0 14K

BigMoney.VIP Powered by Hosting Pokrov