Python Dictionary Key/Value Swap
Posted 2023-03-03 21:55:48
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'}
Search
Categories
- Arts
- Business
- Computers
- Games
- Health
- Home
- Kids and Teens
- Money
- News
- Personal Development
- Recreation
- Regional
- Reference
- Science
- Shopping
- Society
- Sports
- Бизнес
- Деньги
- Дом
- Досуг
- Здоровье
- Игры
- Искусство
- Источники информации
- Компьютеры
- Личное развитие
- Наука
- Новости и СМИ
- Общество
- Покупки
- Спорт
- Страны и регионы
- World
Read More
What Are the Benefits of Offshoring?
In today’s globalized economy, offshoring has become a widely adopted strategy for...
What Is Dynamic Retargeting? A Complete Guide to Personalized Ads and Automated Conversions
Dynamic retargeting is one of the most powerful forms of digital advertising in modern marketing....
How Many Questions Are Too Many?
When gathering feedback—whether through a survey, interview, or conversation—asking...
Is Universal Basic Income a Good Idea?
Is Universal Basic Income a Good Idea?
Universal Basic Income (UBI) is one of the most debated...
How Do I Create a Membership Program? Building a Relationship, Not Just a Revenue Stream
Many organizations start in the same place.
They have customers.
An audience.
Perhaps a loyal...