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'}
Search
Categories
Read More
Жизненные вопросы
Могила светлячков. Grave of the Fireflies. (1988)
Последние дни Второй мировой войны, американская авиация бомбит беззащитные японские города. В...
By Nikolai Pokryshkin 2022-12-05 21:42:35 0 27K
Научная фантастика и фэнтези
Индиана Джонс и последний крестовый поход. Indiana Jones and the Last Crusade. (1989)
На этот раз бесстрашный Индиана разыскивает самую таинственную реликвию в истории человечества...
By Nikolai Pokryshkin 2023-02-01 11:05:49 0 28K
Business
What Strategies Can Help Increase My Follower Count?
Growing your follower count is essential for establishing a strong presence on social media and...
By Dacey Rankins 2025-02-18 15:33:43 0 9K
Horror
The Sixth Sense. (1999)
A frightened, withdrawn Philadelphia boy who communicates with spirits seeks the help of a...
By Leonard Pokrovski 2023-02-15 11:47:16 0 22K
Business
Why Do Growth Rates Mislead?
Growth rates are one of the most common metrics used to describe progress, whether in business,...
By Dacey Rankins 2025-09-09 14:38:42 0 3K

BigMoney.VIP Powered by Hosting Pokrov