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'}
Zoeken
Categorieën
- Arts
- Business
- Computers
- Spellen
- Health
- Home
- Kids and Teens
- Money
- News
- Recreation
- Reference
- Regional
- Science
- Shopping
- Society
- Sports
- Бизнес
- Деньги
- Дом
- Досуг
- Здоровье
- Игры
- Искусство
- Источники информации
- Компьютеры
- Наука
- Новости и СМИ
- Общество
- Покупки
- Спорт
- Страны и регионы
- World
Read More
The Great Dictator. (1940)
Dictator Adenoid Hynkel tries to expand his empire while a poor Jewish barber tries to avoid...
What Is User Behavior Modeling and How Is It Applied?
In today’s digital ecosystem, companies collect massive amounts of data on how users...
Startup.com. (2001)
Traces the birth and failure of new media company govWorks.com.
My Link
How to choose the perfect outfit for the office: style tips
Choosing the perfect outfit for the office can be challenging, especially if you want to look...
Dyslexia: Gene–environment interaction
The contribution of gene–environment interaction to reading disability has been intensely...