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
Personal Finance
When Should I Consider Refinancing My Mortgage?
When Should I Consider Refinancing My Mortgage? Should I Refinance if Rates Drop? What Are the...
Por Leonard Pokrovski 2025-11-17 17:53:52 0 4K
Business
What is recruiting and what do recruiters do: a starter guide
What is recruiting and what do recruiters do: a starter guide  ...
Por Leonard Pokrovski 2024-08-14 17:31:07 0 24K
Business
Retargeting: types, scenarios
Retargeting is actively used in e-commerce. For example, if a consumer searched the internet for...
Por Dacey Rankins 2024-09-18 17:16:51 0 17K
История
Ран. Ran. (1985)
Япония, XVI век. Стареющий правитель Хидэтора объявляет о разделе своих владений между тремя...
Por Nikolai Pokryshkin 2023-02-11 19:18:08 0 24K
Mapas
What are the cards by purpose. Meaning and classification of geographical maps
What are the cards by purpose. Meaning and classification of geographical mapsFor the first time,...
Por FWhoop Xelqua 2023-03-12 15:27:14 0 22K

BigMoney.VIP Powered by Hosting Pokrov