Python Dictionary Key/Value Swap

0
13K

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
Social Issues
Jailbait. (2014)
Anna Nix is sent to a juvenile prison for the murder of her abusive stepfather. In the prison,...
Por Leonard Pokrovski 2023-04-30 16:57:07 0 29K
Business
Psychology of outdoor advertising: how it affects our behavior
  Imagine a city street without bright signs, billboards and glowing screens –...
Por Dacey Rankins 2024-09-18 16:44:30 0 17K
Business
Prepare Thoughtful Questions: Show Interest and Initiative in Interviews
One of the most overlooked opportunities in a job interview comes at the end when the interviewer...
Por Dacey Rankins 2025-06-20 17:07:48 0 8K
Искусство, культура и развлечения
Манк. Mank. (2020)
1940 год. Нью-йоркский острослов Герман Манкевич, когда-то приехавший покорять Голливуд и в целом...
Por Nikolai Pokryshkin 2022-09-25 21:17:14 0 28K
By Region
Australian Media
The media in Australia is an important component of the economy. Australian media is widely...
Por FWhoop Xelqua 2023-03-23 17:35:46 0 24K

BigMoney.VIP Powered by Hosting Pokrov