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
Television
TCT Live TV. Marion, Illinois, United States.
Take a look at the TCT Network and you will find award-winning exclusive programming, live...
By Nikolai Pokryshkin 2022-11-21 18:17:44 0 41K
Economics
Is Econometrics Done in R or Python?
Is Econometrics Done in R or Python? Econometrics, the application of statistical and...
By Leonard Pokrovski 2026-03-25 07:20:10 0 4K
Human Resources
What Industries Use Offshoring the Most?
In the modern global economy, offshoring has become a central strategy for organizations seeking...
By Dacey Rankins 2026-03-30 17:28:05 0 2K
Economics
How does economic theory apply in real life?
How Does Economic Theory Apply in Real Life? Economics has always suffered from a peculiar...
By Leonard Pokrovski 2026-05-07 17:30:29 0 3K
Programming
JavaScript JSON.stringify()
A common use of JSON is to exchange data to/from a web server. When sending data to a web...
By Jesse Thomas 2023-05-10 22:18:01 0 12K

BigMoney.VIP Powered by Hosting Pokrov