Python Dictionary Key/Value Swap

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
Read More
Жизненные вопросы
Эта замечательная жизнь. It's a Wonderful Life. (1946)
Джордж Бейли, владелец кредитной компании в выдуманном американском городке Бедфорд Фоллс,...
By Nikolai Pokryshkin 2022-11-16 08:29:26 0 31K
Marketing and Advertising
How Do You Maintain Engagement in a Community?
Building a community is only the first step. The real challenge lies in sustaining engagement...
By Dacey Rankins 2025-09-26 15:09:41 0 13K
Business
What Are Examples of B2C Companies? They’re the Businesses Quietly Competing for Your Attention Before You Even Realize You’re Ready to Buy
A teenager opens a food delivery app because she’s too tired to cook. A commuter streams...
By Dacey Rankins 2026-05-25 18:45:12 0 2K
Productivity
How often should I repeat a habit?
How Often Should I Repeat a Habit? This question sounds like a scheduling problem. But...
By Michael Pokrovski 2026-05-18 20:13:29 0 1K
Business
Who Writes User Stories?
In Agile product development, user stories are at the heart of how teams capture user needs and...
By Dacey Rankins 2025-08-25 18:46:02 0 6K

BigMoney.VIP Powered by Hosting Pokrov