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
Жизненные вопросы
Мэри и Макс. Mary and Max. (2009)
История отношений 8-летней девочки Мэри Динкль, которая скучает в пригороде далекого...
By Nikolai Pokryshkin 2023-04-06 12:24:30 0 34K
Economics
What is the difference between industrial relations and HR?
What Is the Difference Between Industrial Relations and HR? Industrial relations and human...
By Leonard Pokrovski 2026-07-13 14:18:07 0 883
Human Resources
What Is the Role of Facility Management in an Organization?
Facility management plays a central role in ensuring that an organization’s physical...
By Dacey Rankins 2026-04-15 15:43:44 0 3K
Productivity
How to start a new habit?
How to Start a New Habit? Starting a new habit is usually treated as a motivation problem....
By Michael Pokrovski 2026-05-18 19:47:11 0 1K
Marketing and Advertising
What Is Market Research? Definition, Purpose, and Scope Explained
Introduction: Why Every Business Starts With Understanding the Market Before launching a...
By Dacey Rankins 2025-10-09 18:18:36 0 7K

BigMoney.VIP Powered by Hosting Pokrov