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'}
Site içinde arama yapın
Kategoriler
- Arts
- Business
- Computers
- Oyunlar
- Health
- Home
- Kids and Teens
- Money
- News
- Personal Development
- Recreation
- Regional
- Reference
- Science
- Shopping
- Society
- Sports
- Бизнес
- Деньги
- Дом
- Досуг
- Здоровье
- Игры
- Искусство
- Источники информации
- Компьютеры
- Личное развитие
- Наука
- Новости и СМИ
- Общество
- Покупки
- Спорт
- Страны и регионы
- World
Read More
How to manage subscriptions and unsubscribes to on-site and off-site newsletters
There are 2 pieces of news: bad and good. The bad news is that your subscriber base is degraded...
Elevate Your Viewing Experience with the Smart Base for your TV
Introducing the Smart Base for TV, the revolutionary device designed to enhance your television...
Who Needs to File a Tax Return?
Who Needs to File a Tax Return?
Do I Have to File Even If I Didn’t Make Much...
How Do We Manage Communication and Collaboration in a Partnership?
A business partnership is one of the most powerful forms of collaboration — but also one of...
What Is Public Relations (PR) and Why It Matters for Every Business
Public Relations (PR) is one of the most powerful — yet often misunderstood — tools...