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
Do I Need a Lawyer to Start a Startup?
Starting a business is an exciting and challenging endeavor, but it also comes with legal...
What Questions Do Venture Capitalists Ask Startups?
When pitching to venture capitalists (VCs), startups are often laser-focused on explaining their...
What Does a Product Manager Do?
A product manager (PM) wears many hats, acting as a strategist, communicator, and decision-maker...
Explicit and implicit costs and accounting and economic profit
Key points
Privately owned firms are motivated to earn profits. Profit is the...
What Do You Like Most About Working for This Company?
During a job interview, one insightful question candidates often ask is: "What do you like most...