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
Телевидение
МИР24 прямой эфир.
«Межгосударственная телерадиокомпания «Мир» была создана в 1992 году...
By Nikolai Pokryshkin 2022-10-08 13:17:25 0 34K
Decision Making and Problem Solving
What part of the brain controls creativity?
What Part of the Brain Controls Creativity? Creativity is often treated like a magical...
By Michael Pokrovski 2026-06-24 18:58:21 0 631
Economics
Is capitalism sustainable in the long term?
Is capitalism sustainable in the long term? Capitalism has been the dominant economic system for...
By Leonard Pokrovski 2026-02-13 19:09:16 0 10K
Productivity
What are the best online platforms for learning skills?
What Are the Best Online Platforms for Learning Skills? The question sounds simple, but it hides...
By Michael Pokrovski 2026-05-31 17:08:55 0 2K
Business
Group Cohesiveness: The Glue That Holds Teams Together
In any successful team, there’s often an invisible force at play—something that...
By Dacey Rankins 2025-05-21 14:57:34 0 10K

BigMoney.VIP Powered by Hosting Pokrov