Python Dictionary Key/Value Swap

0
14K

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
Научная фантастика и фэнтези
Властелин колец: Две крепости. The Lord of the Rings: The Two Towers. (2002)
Братство распалось, но Кольцо Всевластья должно быть уничтожено. Фродо и Сэм вынуждены довериться...
Por Nikolai Pokryshkin 2022-11-10 10:38:15 0 46K
Productivity
How to build good habits?
How to Build Good Habits? Most people approach habit building like a motivation problem. They...
Por Michael Pokrovski 2026-05-18 19:45:34 0 2K
Business
Are There Legal or Reputational Risks in a Partnership?
Partnerships can be powerful business structures, enabling shared resources, complementary...
Por Dacey Rankins 2025-12-01 15:31:51 0 14K
Television
RT, live TV. Russia.
RT’s flagship, award-winning English-language channel airs 24/7 from the network’s...
Por Nikolai Pokryshkin 2022-11-14 17:11:18 0 39K
Социальные проблемы
Побег из Шоушенка. The Shawshank Redemption. (1994)
Бухгалтер Энди Дюфрейн обвинён в убийстве собственной жены и её любовника. Оказавшись в тюрьме...
Por Nikolai Pokryshkin 2022-10-24 13:34:22 0 38K

BigMoney.VIP Powered by Hosting Pokrov