Python Dictionary Key/Value Swap

0
12K

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
Read More
Occupational Health and Safety
Occupational Health and Safety
Labor protection and safety at the enterprise include a set of measures aimed at ensuring the...
By FWhoop Xelqua 2023-02-10 18:11:24 0 19K
Business
The Zone of Possible Agreement (ZOPA)
Introduction: Why ZOPA Matters in Every Negotiation Whether you’re negotiating a salary, a...
By Dacey Rankins 2025-11-28 14:34:49 0 2K
Жизненные вопросы
Окно во двор. Rear Window. (1954)
Прикованный из-за сломанной ноги к инвалидному креслу фоторепортер от скуки подглядывает за...
By Nikolai Pokryshkin 2022-12-08 11:50:18 0 29K
Marketing and Advertising
How Effective Is Advertising?
Advertising has existed for centuries, yet one question never disappears: Does advertising...
By Dacey Rankins 2026-01-05 23:49:01 0 1K
Business
How Should Edge Cases Be Handled in User Stories?
When writing user stories in Agile development, most teams naturally focus on the happy...
By Dacey Rankins 2025-08-26 11:46:24 0 6K

BigMoney.VIP Powered by Hosting Pokrov