Python Dictionary Key/Value Swap
Postado 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'}
Pesquisar
Categorias
- Arts
- Business
- Computers
- Jogos
- Health
- Início
- Kids and Teens
- Money
- News
- Personal Development
- Recreation
- Regional
- Reference
- Science
- Shopping
- Society
- Sports
- Бизнес
- Деньги
- Дом
- Досуг
- Здоровье
- Игры
- Искусство
- Источники информации
- Компьютеры
- Личное развитие
- Наука
- Новости и СМИ
- Общество
- Покупки
- Спорт
- Страны и регионы
- World
Leia Mais
How Much Should I Save for Retirement — and When Can I Retire?
How Much Should I Save for Retirement — and When Can I Retire?
A Practical Guide to...
What Skills Do Successful Startup Founders Have?
Starting a business and leading a startup is no easy feat. The path to success is often filled...
How Long Should My Marketing Videos Be? Finding the Perfect Duration for Every Platform and Purpose
Introduction
Video is one of the most engaging and effective forms of marketing content today,...
What Equipment Do I Need to Begin a Podcast?
Introduction
Podcasting is one of the most democratic forms of media in existence today. Anyone...
Miss Marx (2020)
Bright, intelligent, passionate and free, Eleanor is Karl Marx's youngest daughter. Among the...