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
Business
How Do You Build or Increase Your Own Influence?
Influence is one of the most valuable skills you can develop. It shapes how people listen to you,...
By Dacey Rankins 2025-11-25 11:02:27 0 528
История
Моё тело вас закопает. My Body Will Bury You. (2021)
1860 год. Накануне объединения Италии в единое королевство южную часть полуострова наводнили...
By Nikolai Pokryshkin 2022-09-11 21:25:00 0 27K
Programming
Python Comparison
Comparisons are used to compare two values in a code. For example, "==", "!=", ">", "<",...
By Jesse Thomas 2023-02-15 21:23:46 0 11K
Yard, Deck, and Table Games
Why Do Children Play Yard Games in the Modern World
Why should children play yard games? Modern children are increasingly spending their free time...
By Dacey Rankins 2024-04-09 14:40:39 0 14K
Bedrijvengids
The Treasure of the Sierra Madre. (1948)
Two down-on-their-luck Americans searching for work in 1920s Mexico convince an old prospector to...
By Leonard Pokrovski 2023-02-20 17:13:40 0 21K

BigMoney.VIP Powered by Hosting Pokrov