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'}
Pesquisar
Categorias
Leia Mais
Mental Health
Psychosis: Culture
Cross-cultural studies into schizophrenia have found that individual experiences of psychosis and...
Por Kelsey Rodriguez 2023-06-09 20:02:28 0 11K
Искусство, культура и развлечения
Амадей. Amadeus. (1984)
1781 год. Антонио Сальери успешно справляется с обязанностями придворного композитора при...
Por Nikolai Pokryshkin 2022-12-27 17:57:07 0 40K
Personal Finance
How Do I Stay Motivated to Save Money?
How Do I Stay Motivated to Save Money? Strategies, Psychological Tricks, and Visual Systems to...
Por Leonard Pokrovski 2025-12-09 16:53:36 0 2K
Business
What Are the CEO's Major Accomplishments?
A CEO’s major accomplishments serve as powerful indicators of their leadership capability,...
Por Dacey Rankins 2025-06-30 14:10:46 0 8K
Central America
Central America Yachts
With such "brands" as the Caribbean Sea and the Panama Canal, Central America, it would seem,...
Por FWhoop Xelqua 2023-03-17 18:15:06 0 23K

BigMoney.VIP Powered by Hosting Pokrov