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
Social Issues
The Departed. (2006)
An undercover cop and a mole in the police attempt to identify each other while infiltrating an...
By Leonard Pokrovski 2022-12-01 20:36:46 0 25K
Business
How to choose quality office supplies?
It wasn’t an expensive pen. But it was the only one within reach when a signature was...
By Dacey Rankins 2026-05-05 19:54:47 0 3K
Business
How Does the Build-Measure-Learn Feedback Loop Work?
The Build-Measure-Learn feedback loop is a core principle of the Lean Startup methodology,...
By Dacey Rankins 2025-03-07 13:52:36 0 21K
Social Issues
The Intern. (2015)
Seventy-year-old widower Ben Whittaker has discovered that retirement isn't all it's cracked up...
By Leonard Pokrovski 2023-05-25 18:58:50 0 27K
Business
What Are the Different Retail Business Models? Understanding the Strategies Behind Modern Commerce
A customer buys a pair of running shoes. That single purchase appears simple. A product is...
By Dacey Rankins 2026-07-14 18:39:57 0 2K

BigMoney.VIP Powered by Hosting Pokrov