Python Dictionary Key/Value Swap

0
10Кб

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'}
Поиск
Категории
Больше
Business
Why Do Startups Fail?
Starting a business is an exciting journey filled with opportunities, but the reality is that...
От Dacey Rankins 2025-03-17 18:36:48 0 6Кб
Business
What Is the Role of Competitive Analysis in Product Strategy?
In product management, building a winning product is never done in isolation. No matter how...
От Dacey Rankins 2025-08-21 16:41:26 0 271
История
1917. (2019)
6 апреля 1917 года, разгар Первой мировой войны, Западный фронт на севере Франции. Британский...
От Nikolai Pokryshkin 2023-02-01 22:53:04 0 39Кб
Business
What Are Examples of Constructive Feedback?
Constructive feedback is essential for learning, growth, and improving performance—whether...
От Dacey Rankins 2025-07-29 17:37:13 0 1Кб
Animal
"Veterinary medicine is exciting!"
"Veterinary medicine is exciting!" Veterinary medicine is a field of scientific knowledge and...
От Leonard Pokrovski 2024-04-10 21:53:12 0 17Кб

BigMoney.VIP Powered by Hosting Pokrov