Python Dictionary Key/Value Swap
Posted 2023-03-03 21:55:48
0
9K
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
- Recreation
- Reference
- Regional
- Science
- Shopping
- Society
- Sports
- Бизнес
- Деньги
- Дом
- Досуг
- Здоровье
- Игры
- Искусство
- Источники информации
- Компьютеры
- Наука
- Новости и СМИ
- Общество
- Покупки
- Спорт
- Страны и регионы
- World
Read More
How Can I Provide Effective Feedback?
Delivering Constructive and Actionable Feedback in Professional and Personal Relationships
Why...
The Thrilling World of Handball: A Sport of Skill, Speed, and Strategy
Handball, a dynamic and fast-paced sport, combines the finesse of basketball, the physicality of...
What are market-oriented environmental tools?
Key points
The three main categories of market-oriented environmental policies are...
Conflict Resolution Techniques [FAQs Guide]
Conflict is an inevitable part of life—whether at work, school, home, or in social...
How Do I Determine My Startup Costs and Other Expenses?
Starting a new business can be an exciting yet overwhelming experience. One of the most crucial...