Python Dictionary Key/Value Swap

0
5K

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
Biology
Biology
Biology (Greek: βιολογία; from Ancient Greek:...
Por FWhoop Xelqua 2023-03-03 16:42:32 0 12K
Business
5 reasons to work if you already have enough money
Many work primarily for money.But this is not everyone's only motivation. Many people advise...
Por Dacey Rankins 2024-09-24 13:46:25 0 8K
Social Issues
Taxi Driver. (1976)
A mentally unstable veteran works as a nighttime taxi driver in New York City, where the...
Por Leonard Pokrovski 2023-01-28 17:27:40 0 11K
Programming
Remove Keys from Dictionary - Python
In order to delete keys from the diction, you would have to use 'del'. Here's an example:x ...
Por Jesse Thomas 2023-02-23 23:16:41 0 5K
Financial Services
What are public goods?
Key points A public good has two key characteristics: it is...
Por Mark Lorenzo 2023-05-12 19:29:33 0 5K
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov