Python Dictionary Key/Value Swap

0
8K

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
Green Book. (2018)
A working-class Italian-American bouncer becomes the driver for an African-American classical...
Por Leonard Pokrovski 2023-02-08 12:06:53 0 14K
Reproductive Health
Reproductive Health: Understanding Its Importance for Individuals and Society
Reproductive health is a cornerstone of overall well-being, encompassing a wide range of...
Por Dacey Rankins 2024-11-05 15:52:56 0 4K
Squash
Exploring the Elegance and Intensity of Squash: A Sport for the Agile and Strategic
Squash, often dubbed the "fastest racket sport," is an exhilarating and demanding game that...
Por Dacey Rankins 2024-07-10 16:23:11 0 14K
Социальные проблемы
Олдбой. Oldboy. (2003)
1988 год. Обыкновенный и ничем непримечательный бизнесмен О Дэ-cу в день трёхлетия дочери по пути...
Por Nikolai Pokryshkin 2022-12-26 12:04:09 0 20K
Business
What are the best ways to scale a business?
Scaling a business is an exciting milestone, but it requires careful planning, strategic...
Por Dacey Rankins 2025-03-17 18:21:45 0 4K
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov