Python Dictionary Key/Value Swap

0
13K

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
What Is the “Safe” Withdrawal Rate From Retirement?
What Is the “Safe” Withdrawal Rate From Retirement? One of the biggest questions...
By Leonard Pokrovski 2025-11-26 20:11:57 0 2K
Personal Finance
How Many Credit Cards Should You Have — and How Does a Credit Card Work?
How Many Credit Cards Should You Have — and How Does a Credit Card Work? Credit cards can...
By Leonard Pokrovski 2025-11-19 22:01:18 0 3K
Business
How Often Should I Ask for Feedback?
Asking for feedback is one of the most powerful habits a professional or leader can...
By Dacey Rankins 2025-07-28 14:51:58 0 3K
Business
How Does Mentoring Differ from Coaching?
Mentoring and coaching are often used interchangeably, but they are fundamentally different in...
By Dacey Rankins 2025-07-07 14:40:14 0 5K
Социальные проблемы
Двойная страховка. Double Indemnity. (1944)
Сластолюбивый страховой агент Уолтер Нефф отправляется продлить полис на машину некоему мистеру...
By Nikolai Pokryshkin 2023-01-20 18:34:24 0 28K

BigMoney.VIP Powered by Hosting Pokrov