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'}
Buscar
Categorías
Read More
Искусство, культура и развлечения
Душа. Soul. (2020)
Уже немолодой школьный учитель музыки Джо Гарднер всю жизнь мечтал выступать на сцене в составе...
By Nikolai Pokryshkin 2022-09-22 21:41:34 0 36K
Business
When Can I Expect to Hear Back?
One of the most common and anxiety-inducing questions from candidates during interviews is,...
By Dacey Rankins 2025-08-07 17:37:26 0 7K
Business
What Is the Scope—And How Will We Measure It?
Determining Boundaries and Success Metrics for the Project** Project scope defines the...
By Dacey Rankins 2025-07-12 20:48:16 0 5K
Искусство, культура и развлечения
Мадам Парфюмер. Perfumes. (2020)
Анна Вальберг — знаменитость в мире парфюмерии. Она живет как дива, ведет себя как эгоистка...
By Nikolai Pokryshkin 2022-09-30 21:26:20 0 26K
Personal Finance
When Is the Best Age to Take Social Security?
When Is the Best Age to Take Social Security? 62 vs. Full Retirement Age vs. 70 Deciding when...
By Leonard Pokrovski 2025-12-11 20:14:55 0 2K

BigMoney.VIP Powered by Hosting Pokrov