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
Научная фантастика и фэнтези
Индиана Джонс: В поисках утраченного ковчега. Indiana Jones and the Raiders of the Lost Ark. (1981)
Известный археолог и специалист по оккультным наукам доктор Джонс получает опасное задание от...
By Nikolai Pokryshkin 2022-12-13 18:09:48 0 27K
Sport
Senna (2010)
A documentary on Brazilian Formula One racing driver Ayrton Senna, who won the F1 world...
By Leonard Pokrovski 2023-07-04 19:54:58 0 28K
Money
What is Inflation?
What is Inflation? Inflation is a term you’ve probably heard in news headlines, economic...
By Leonard Pokrovski 2025-09-17 20:03:15 0 3K
Extreme Sports
The Thrill of Extreme Sports: Pushing Limits and Defying Gravity
The Thrill of Extreme Sports: Pushing Limits and Defying Gravity Extreme sports have long...
By Leonard Pokrovski 2024-06-22 02:25:21 0 17K
Business
Promising business in 2024
It will be possible to open a business and run it successfully, given the situation on the market...
By Dacey Rankins 2024-03-11 17:50:24 0 27K

BigMoney.VIP Powered by Hosting Pokrov