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
Социальные проблемы
Старикам тут не место. No Country for Old Men. (2007)
Обычный работяга обнаруживает в пустыне гору трупов, набитый героином грузовик и соблазнительную...
By Nikolai Pokryshkin 2023-02-22 18:21:10 0 23K
Business
What Role Does Non-Verbal Communication Play?
Introduction Communication is far more than the words we speak or write. Studies suggest that a...
By Dacey Rankins 2025-11-20 15:23:51 0 2K
Телевидение
Христианский телеканал GNC
GNC — Первый Христианский спутниковый телеканал на русском и украинском языках,...
By Nikolai Pokryshkin 2022-08-30 10:47:57 0 29K
Business
What Equipment is Necessary for Remote Work?
As remote work continues to rise in popularity, ensuring that employees have the proper equipment...
By Dacey Rankins 2025-01-29 16:58:23 0 14K
Социальные проблемы
Карты, деньги, два ствола. Lock, Stock and Two Smoking Barrels. (1998)
Четверо молодых парней накопили каждый по 25 тысяч фунтов, чтобы один из них мог сыграть в карты...
By Nikolai Pokryshkin 2023-03-04 12:57:43 0 34K

BigMoney.VIP Powered by Hosting Pokrov