Python Dictionary Key/Value Swap
Posted 2023-03-03 21:55:48
0
13K
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 is a video presentation and why do you need it?
Video presentation is becoming more and more popular among businessmen and companies whose goal...
What Is the Role of Simulation and Risk Analysis?
Simulation and risk analysis are core tools in Management Science, especially in uncertain and...
Growing Your Business: Strategies for Scaling and Success
Growing a business is an exciting yet challenging process that requires strategic planning,...
The Thin Blue Line (1988)
A film that successfully argued that a man was wrongly convicted for murder by a corrupt justice...
How Much Do YouTubers Make?
How Much Do YouTubers Make?
YouTube has evolved from a video-sharing platform into a global hub...