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
Personal Finance
When Can—and Should—I Retire?
When Can—and Should—I Retire? Understanding the “Right Time” Through...
By Leonard Pokrovski 2025-11-24 15:22:37 0 6K
Business
How Do Emotions Affect Buying Behavior?
Most consumers like to believe they make rational purchasing decisions. It's an appealing idea....
By Dacey Rankins 2026-05-30 20:10:13 0 1K
Productivity
What are small changes that make a big difference?
Small changes can have a disproportionately large impact on your life, productivity, health,...
By Michael Pokrovski 2026-03-24 23:27:44 0 4K
Marketing and Advertising
How Do I Write Effective Facebook Ad Copy?
Facebook advertising is not won by targeting alone. Two advertisers can target the exact same...
By Dacey Rankins 2026-01-22 19:40:42 0 3K
Жизненные вопросы
Общество мертвых поэтов. Dead Poets Society. (1989)
Джон Китинг — новый преподаватель английской словесности в консервативном американском...
By Nikolai Pokryshkin 2023-04-09 12:11:36 0 41K

BigMoney.VIP Powered by Hosting Pokrov