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
Business
What Are Common Pitching Mistakes?
Pitching — whether to investors, customers, partners, or the media — is a skill most...
By Dacey Rankins 2025-12-03 15:45:27 0 8K
Социальные проблемы
Зелёная книга. Green Book. (2018)
1960-е годы. После закрытия нью-йоркского ночного клуба на ремонт вышибала Тони по прозвищу...
By Nikolai Pokryshkin 2023-02-07 13:15:09 0 33K
Marketing and Advertising
What Is a Good Cost Per Click (CPC)?
Cost per click (CPC) is one of the most critical metrics in pay-per-click (PPC) advertising. It...
By Dacey Rankins 2026-02-04 17:49:19 0 7K
Human Resources
How Does HR Recruit Employees?
Recruiting the right employees is one of the most important responsibilities of Human Resources...
By Dacey Rankins 2026-03-19 18:34:17 0 4K
Decision Making and Problem Solving
How do you solve lateral thinking puzzles?
The Architecture of the Intellectual Pivot The mind is a pattern-making machine. It is...
By Michael Pokrovski 2026-07-09 10:09:08 0 591

BigMoney.VIP Powered by Hosting Pokrov