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
The Departed. (2006)
An undercover cop and a mole in the police attempt to identify each other while infiltrating an...
How to choose quality office supplies?
It wasn’t an expensive pen.
But it was the only one within reach when a signature was...
How Does the Build-Measure-Learn Feedback Loop Work?
The Build-Measure-Learn feedback loop is a core principle of the Lean Startup methodology,...
The Intern. (2015)
Seventy-year-old widower Ben Whittaker has discovered that retirement isn't all it's cracked up...
What Are the Different Retail Business Models? Understanding the Strategies Behind Modern Commerce
A customer buys a pair of running shoes.
That single purchase appears simple.
A product is...