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
Ужасы
Дальний космос. Stowaway (2021)
Экипаж космического корабля, направляющегося на Марс, обнаруживает на борту случайного пассажира...
By Nikolai Pokryshkin 2022-09-18 22:13:39 0 64K
Business
How Important Are Soft Skills and Growth Opportunities?
In today’s competitive job market, employers are realizing that technical skills alone...
By Dacey Rankins 2025-08-12 14:13:13 0 5K
Social Issues
The Trial of the Chicago 7. (2020)
The story of 7 people on trial stemming from various charges surrounding the uprising at the 1968...
By Leonard Pokrovski 2022-10-12 19:41:24 0 29K
Business
Why Do I Need a Business Consulting Service?
In today’s competitive and ever-evolving business environment, companies face various...
By Dacey Rankins 2025-02-10 18:11:31 0 12K
Horror
The Little Things (2021)
Kern County Deputy Sheriff Joe Deacon is sent to Los Angeles for what should have been a quick...
By Leonard Pokrovski 2022-09-24 18:43:08 0 34K

BigMoney.VIP Powered by Hosting Pokrov