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'}
Site içinde arama yapın
Kategoriler
Read More
Chemistry
TOP 10 discoveries and events in the chemical industry in 2021
Another difficult year is behind us. Political, socio-economic and climatic changes every day...
By FWhoop Xelqua 2023-01-25 16:19:56 0 23K
Business
How Do I Tailor Feedback for Bosses, Peers, and Reports?
Giving feedback isn’t one-size-fits-all. How you deliver feedback should vary depending on...
By Dacey Rankins 2025-07-29 17:38:25 0 4K
Data Formats
File format
A file format is a specification of the structure of data recorded in a computer file. The file...
By Dacey Rankins 2024-03-21 15:37:18 0 33K
Business
How Can Businesses Handle Ethical Dilemmas?
In today's fast-paced and ever-evolving business world, ethical dilemmas are an inevitable...
By Dacey Rankins 2025-02-03 15:54:35 0 14K
Жизненные вопросы
Вверх. Up. (2009)
78-летний ворчун Карл Фредриксен считает, что жизнь обходит его стороной. Чтобы сдержать...
By Nikolai Pokryshkin 2023-01-26 09:12:26 0 36K

BigMoney.VIP Powered by Hosting Pokrov