Python Dictionary Key/Value Swap

0
11K

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
Recreation
The Art of Shopping as Recreation: Exploring the Joy Beyond Purchases
Shopping has transcended its utilitarian roots to become a leisure activity in its own right. No...
By Dacey Rankins 2024-06-03 18:24:16 0 16K
Apartment Living
The Pros and Cons of Apartment Living: A Modern Lifestyle Choice
Introduction Apartment living has become a popular choice for many, especially in urban areas...
By Dacey Rankins 2024-10-07 17:33:13 0 18K
Personal Finance
Personal Finance: A Guide to Financial Health and Empowerment
Personal finance is a vital aspect of everyday life, encompassing the management of money,...
By Dacey Rankins 2024-10-17 19:57:02 0 26K
Социальные проблемы
Новые времена. Modern Times. (1936)
«Новые времена» - это эмоциональный, выдержанный в комическом ключе, отклик на...
By Nikolai Pokryshkin 2022-12-06 22:41:12 0 24K
Business
How Does the Role of a COO Differ Between Organizations?
The role of a Chief Operating Officer (COO) is crucial in the smooth operation and growth of an...
By Dacey Rankins 2025-01-28 15:31:52 0 12K

BigMoney.VIP Powered by Hosting Pokrov