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'}
Zoeken
Categorieën
Read More
Regional
China as a global partner
The first series of broadcasts is dedicated to China. Alexey Kalinin, Vice-Rector for Research at...
By FWhoop Xelqua 2023-07-22 18:10:41 0 34K
Arts, Culture and Entertainment
Crescendo (2020)
An outwardly ambitious and charismatic opera singer fights to maintain her stature while silently...
By Leonard Pokrovski 2022-10-02 20:28:40 0 30K
Personal Finance
What Is a Good Credit Score? What Number Counts as “Good” or “Excellent” Credit?
What Is a Good Credit Score? What Number Counts as “Good” or “Excellent”...
By Leonard Pokrovski 2025-11-17 17:57:13 0 1K
Ужасы
Дьявол в деталях. The Little Things. (2021)
Октябрь 1990 года. Уже немолодого заместителя окружного шерифа Джо Дикона по прозвищу Дики...
By Nikolai Pokryshkin 2022-09-21 17:31:15 0 38K
Business
What is a Business Plan?
A business plan is a comprehensive document that outlines the vision, mission, goals, and...
By Dacey Rankins 2024-12-17 14:46:53 0 11K

BigMoney.VIP Powered by Hosting Pokrov