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
Health
13 gift options for a person leading a healthy lifestyle
Leading a healthy lifestyle has become a fashionable trend among the population of the whole...
By FWhoop Xelqua 2022-10-17 13:05:47 0 41K
Horror
Survival Island. (2005)
Three people - a rich couple and a crew member - are shipwrecked on a tropical island and their...
By Leonard Pokrovski 2023-04-28 19:26:28 0 26K
Social Issues
The Godfather. (1972)
The Godfather "Don" Vito Corleone is the head of the Corleone mafia family in New York. He is at...
By Leonard Pokrovski 2022-10-31 19:32:39 0 28K
Business
What Are Your Strengths and Weaknesses? A Window into Self-Awareness and Growth Potential
The interview question “What are your strengths and weaknesses?” remains a classic...
By Dacey Rankins 2025-06-25 12:42:58 0 6K
Social Issues
Taxi Driver. (1976)
A mentally unstable veteran works as a nighttime taxi driver in New York City, where the...
By Leonard Pokrovski 2023-01-28 17:27:40 0 19K

BigMoney.VIP Powered by Hosting Pokrov