Python Dictionary Key/Value Swap
Posted 2023-03-03 21:55:48
0
12K
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'}
Buscar
Categorías
- Arts
- Business
- Computers
- Juegos
- Health
- Home
- Kids and Teens
- Money
- News
- Recreation
- Reference
- Regional
- Science
- Shopping
- Society
- Sports
- Бизнес
- Деньги
- Дом
- Досуг
- Здоровье
- Игры
- Искусство
- Источники информации
- Компьютеры
- Наука
- Новости и СМИ
- Общество
- Покупки
- Спорт
- Страны и регионы
- World
Read More
Аферистка. I Care a Lot. (2021)
Марла руководствуется принципом «побеждает сильнейший». Женщина организовала...
How to Optimize for Mobile (User Experience, Responsive Design, Page Speed)
Introduction
In the digital age, mobile optimization is no longer optional—it’s the...
Central America Yachts
With such "brands" as the Caribbean Sea and the Panama Canal, Central America, it would seem,...
A Beautiful Mind. (2001)
After John Nash, a brilliant but asocial mathematician, accepts secret work in cryptography, his...
Is This a New Position or Am I Replacing Someone?
Asking whether a role is newly created or replacing a previous employee gives candidates valuable...