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
Why Is Inflation Rising?
Why Is Inflation Rising? Inflation—the rate at which the general level of prices for goods...
By Leonard Pokrovski 2026-04-18 01:29:32 0 4K
Business
How Long Does It Take to See Results From Business Development Efforts?
Business development (BD) is one of the most misunderstood functions in any organization. People...
By Dacey Rankins 2025-11-17 18:37:51 0 11K
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
Business
What Is the Pricing Model for PaaS?
A CFO once joined a product strategy meeting with what seemed like a reasonable request. He...
By Dacey Rankins 2026-07-07 11:22:47 0 3K
Business
Is Affiliate Marketing Saturated?
Every few months, someone declares affiliate marketing dead. Usually from a YouTube thumbnail...
By Dacey Rankins 2026-05-18 15:49:00 0 2K

BigMoney.VIP Powered by Hosting Pokrov