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
Mental Health
ADHD: Controversy
ADHD, its diagnosis, and its treatment have been controversial since the 1970s. The controversies...
By Kelsey Rodriguez 2023-05-09 17:30:57 0 11K
Financial Services
Decisions with a budged constraint
Key points The budget constraint is the boundary of...
By Mark Lorenzo 2023-02-13 15:15:43 0 16K
Programming
Proxy Webserver in Python
Socket programming in python is very user friendly as compared to c. The programmer need not...
By Jesse Thomas 2023-03-13 22:09:22 0 11K
Marketing and Advertising
Why Digital Marketing Is Important
Introduction: The Shift to Digital The way people shop, communicate, and consume information has...
By Dacey Rankins 2025-10-01 14:46:56 0 2K
Искусство, культура и развлечения
Новый кинотеатр «Парадизо». Cinema Paradiso. (1988)
Это рассказ о счастливых днях, когда итальянское кино еще не знало о том, что такое «кризис...
By Nikolai Pokryshkin 2022-12-09 12:46:05 0 30K

BigMoney.VIP Powered by Hosting Pokrov