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
Economics
How much government involvement exists in free enterprise?
How Much Government Involvement Exists in Free Enterprise? The Question Nobody Likes to Answer...
By Leonard Pokrovski 2026-06-03 01:39:31 0 4K
Financial Services
Keynes’ Law and Say’s Law in the AD/AS model
Key points The short-run aggregate supply, or SRAS, curve can be divided into...
By Mark Lorenzo 2023-03-14 19:27:23 0 15K
Mental Health
Autism Spectrum: Repetitive Behaviours
ASD includes a wide variety of characteristics. Some of these include behavioral characteristics...
By Kelsey Rodriguez 2023-02-16 15:28:10 0 12K
Economics
What Careers Are Available in Commerce?
What Careers Are Available in Commerce? Commerce is one of the most practical and flexible...
By Leonard Pokrovski 2026-02-19 02:28:54 0 6K
Economics
What Is Economic Policy?
What Is Economic Policy? Economic policy refers to the actions, strategies, and decisions taken...
By Leonard Pokrovski 2026-04-16 17:24:48 0 2K

BigMoney.VIP Powered by Hosting Pokrov