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
Understanding Computer Security: Protecting Your Digital World
In today’s increasingly digital world, computer security has become an essential aspect of...
What Are the Best B2B Business Models? Most Companies Choose Revenue Structures They Secretly Can’t Sustain
A founder once explained his business model to me with extraordinary confidence.
By the end of...
Project Management Fundamentals: What Should You Focus On?
Project Management Fundamentals: What Should You Focus On?
"Focus on...
What Does PPC Stand For?
PPC is one of the most commonly used terms in digital marketing, yet many people encounter it...
Unraveling the Mysteries: A Dive into the Methods of Science
Science, often portrayed as a beacon of objectivity, is the systematic pursuit of knowledge...