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
Is YouTube Advertising Effective in 2026?
In a digital landscape saturated with ads, privacy changes, AI-driven algorithms, and shorter...
What Are Scalable Business Models?
Most businesses can grow.
Far fewer can scale.
That distinction sounds semantic until you watch...
What supplies are needed for working from home?
At first, it seemed temporary.
A laptop placed between a coffee mug and yesterday’s mail....
What Should Be Included in a Business Biography?
A business biography is a professional narrative that showcases a person’s career journey,...
What Are Common Challenges Startups Face?
Starting a new business is an exciting venture, but it’s also filled with numerous...