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
Gambling
Victims of gambling. Is gambling for money a vice or entertainment?
Gambling has been around since time immemorial. Craps were played in Babylon, Ancient Egypt and...
By Michael Pokrovski 2024-04-05 00:33:39 0 21K
Decision Making and Problem Solving
Why is decision making important in management?
Why Is Decision Making Important in Management? The Discipline That Shapes Every Organizational...
By Michael Pokrovski 2026-06-30 19:06:54 0 542
Social Issues
The Shawshank Redemption. (1994)
Chronicles the experiences of a formerly successful banker as a prisoner in the gloomy jailhouse...
By Leonard Pokrovski 2022-10-30 19:43:08 0 32K
Business
How Can I Foster Intrinsic Motivation in My Team? Fueling Purpose, Autonomy, and Mastery at Work
As a leader, one of the most powerful questions you can ask is:“How can I foster intrinsic...
By Dacey Rankins 2025-07-16 15:50:18 0 17K
Decision Making and Problem Solving
What is a nudge in behavioral economics?
What Is a Nudge in Behavioral Economics? The Choice You Never Noticed Imagine entering a...
By Michael Pokrovski 2026-06-10 11:59:37 0 3K

BigMoney.VIP Powered by Hosting Pokrov