Python Dictionary Key/Value Swap

0
12K

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'}
Zoeken
Categorieën
Read More
Business
How Can I Attract and Retain Talent in My Startup?
Attracting and retaining top talent is one of the most crucial aspects of growing a successful...
By Dacey Rankins 2025-03-04 14:38:37 0 13K
Mental Health
Dyslexia: Pathophysiology
For most people who are right-hand dominant, the left hemisphere of their brain is more...
By Kelsey Rodriguez 2023-06-26 17:18:55 0 11K
Social Issues
Green Book. (2018)
A working-class Italian-American bouncer becomes the driver for an African-American classical...
By Leonard Pokrovski 2023-02-08 12:06:53 0 19K
Travel
Named the most visited attractions in the world
The authoritative publication Travel + Leisure has published a list of the main tourist...
By FWhoop Xelqua 2023-01-03 14:47:15 0 23K
Business
How Do You Handle Rejections? Maintaining Professionalism and Empathy in Candidate Communication
Rejection is an inevitable part of the hiring process, but how it’s handled can...
By Dacey Rankins 2025-06-25 12:50:42 0 5K

BigMoney.VIP Powered by Hosting Pokrov