Python Dictionary Key/Value Swap

0
12K

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
How Can I Earn Money Through Features Like Super Chat, Super Stickers, or Channel Memberships?
In addition to traditional ad revenue, YouTube offers several interactive monetization features...
By Dacey Rankins 2025-02-19 14:43:28 0 14K
Business
How Do I Pitch Without Sounding Salesy?
One of the biggest fears presenters have — whether they’re startup founders,...
By Dacey Rankins 2025-12-03 15:47:54 0 3K
Programming
Python Booleans
Booleans are expressions that evaluates if it's true or false. For example, if you compare 2...
By Jesse Thomas 2023-02-24 21:41:56 0 11K
Жизненные вопросы
Вверх. Up. (2009)
78-летний ворчун Карл Фредриксен считает, что жизнь обходит его стороной. Чтобы сдержать...
By Nikolai Pokryshkin 2023-01-26 09:12:26 0 37K
Business
Outsourcing
Outsourcing is the transfer by an organization on the basis of a contract of certain types or...
By Dacey Rankins 2024-09-20 21:42:32 0 26K

BigMoney.VIP Powered by Hosting Pokrov