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
Business process
A business process is a set of interrelated tasks and activities aimed at achieving a certain...
By Dacey Rankins 2024-07-16 18:48:34 0 17K
Programming
Python Regex
A RegEx, or Regular Expression, is a sequence of characters that forms a search...
By Jesse Thomas 2023-04-05 19:02:13 0 13K
Economics
Income inequality in Europe
Income Inequality in Europe Income inequality in Europe refers to the uneven distribution of...
By Leonard Pokrovski 2026-07-02 18:07:39 0 2K
Economics
Industrial relations PDF
Industrial Relations PDF Introduction Industrial relations (IR) is the study and practice of...
By Leonard Pokrovski 2026-07-21 00:06:50 0 2K
Business
What Are the Biggest Challenges Entrepreneurs Face?
Entrepreneurship is an exciting and rewarding journey, but it also comes with its fair share of...
By Dacey Rankins 2025-03-17 18:41:00 0 13K

BigMoney.VIP Powered by Hosting Pokrov