Python Dictionary Key/Value Swap
Posted 2023-03-03 21:55:48
0
9K
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'}
Zoeken
Categorieën
- Arts
- Business
- Computers
- Spellen
- Health
- Home
- Kids and Teens
- Money
- News
- Recreation
- Reference
- Regional
- Science
- Shopping
- Society
- Sports
- Бизнес
- Деньги
- Дом
- Досуг
- Здоровье
- Игры
- Искусство
- Источники информации
- Компьютеры
- Наука
- Новости и СМИ
- Общество
- Покупки
- Спорт
- Страны и регионы
- World
Read More
Citizenfour (2014)
A documentarian and a reporter travel to Hong Kong for the first of many meetings with Edward...
Embracing the Benefits of Multi-Sport Participation
In the world of athletics, the concept of specializing in a single sport from a young age has...
10 MOST WIDELY READ NEWSPAPERS IN THE WORLD
Over time, it becomes important for newspaper owners to publish their contents in accordance with...
Living History Recreation: Bringing the Past to Life Through Timeless Experience
Living History Recreation: Bringing the Past to Life Through Timeless Experience
In the vast...
What Are the Startup Costs and How Can I Finance My Business?
Starting a business is an exciting journey, but it comes with financial challenges that need...