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
Телевидение
Прямой эфир ТНТ
ТНТ онлайн. Один из наиболее популярных телеканалов. Телеканал ТНТ известен такими проектами как...
Por Nikolai Pokryshkin 2022-09-28 13:07:23 0 35K
Economics
What Is the Difference Between Capitalism and Communism?
What Is the Difference Between Capitalism and Communism? Capitalism and communism are two...
Por Leonard Pokrovski 2026-02-09 22:01:29 0 1K
Softball
Softball: A Celebration of Skill, Strategy, and Team Spirit
Softball: A Celebration of Skill, Strategy, and Team Spirit Softball, a dynamic sport born from...
Por Leonard Pokrovski 2024-07-10 09:15:48 0 23K
Ужасы
Заклятие 3: По воле дьявола. The Conjuring: The Devil Made Me Do It. (2021)
18 июля 1981 года Эд и Лоррейн Уоррены присутствовали при проведении экзорцизма над маленьким...
Por Nikolai Pokryshkin 2022-09-19 20:53:22 0 41K
Жизненные вопросы
Вверх. Up. (2009)
78-летний ворчун Карл Фредриксен считает, что жизнь обходит его стороной. Чтобы сдержать...
Por Nikolai Pokryshkin 2023-01-26 09:12:26 0 37K

BigMoney.VIP Powered by Hosting Pokrov