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
Business
General Director: Job Responsibilities
General Director: Job Responsibilities Is the job description of the general director a...
Por Leonard Pokrovski 2024-08-13 22:42:08 0 18K
Научная фантастика и фэнтези
Терминатор 2: Судный день. Terminator 2: Judgment Day. (1991)
Прошло более десяти лет с тех пор, как киборг из 2029 года пытался уничтожить Сару Коннор —...
Por Nikolai Pokryshkin 2022-11-22 16:19:09 0 24K
Научная фантастика и фэнтези
Звёздные войны: Эпизод 5 — Империя наносит ответный удар. Star Wars: Episode V - The Empire Strikes Back. (1980)
Борьба за Галактику обостряется в пятом эпизоде космической саги. Войска Императора начинают...
Por Nikolai Pokryshkin 2022-11-12 13:20:55 0 36K
Stories
For a Few Dollars More. (1965)
Two bounty hunters with the same intentions team up to track down an escaped Mexican outlaw....
Por Leonard Pokrovski 2023-02-02 14:34:20 0 27K
Business
What Is a Sales Funnel and How Does It Work?
The sales funnel is one of the most important concepts in sales and marketing, yet it’s...
Por Dacey Rankins 2025-12-19 19:17:32 0 3K

BigMoney.VIP Powered by Hosting Pokrov