Python Dictionary Key/Value Swap

0
8K

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
Mental Health
Dementia: Prodromal
Pre-dementia states considered as prodromal are mild cognitive impairment (MCI), and mild...
By Kelsey Rodriguez 2023-07-18 18:47:51 0 9K
Multimedia
The Evolution and Impact of Multimedia in Modern Communication
Introduction In an era dominated by rapid technological advancements, multimedia has emerged as...
By Dacey Rankins 2024-10-14 18:08:13 0 14K
Телевидение
Прямой эфир телеканала Пятница
Телеканал Пятница для тех, кто охотится за впечатлениями и хочет попробовать мир на вкус....
By Nikolai Pokryshkin 2022-10-04 14:16:15 1 22K
Books
The most read books in the world
Top 100 best-selling individual books and book series worldwide for all time. This list is...
By FWhoop Xelqua 2022-10-28 18:03:33 0 16K
Business
How to choose the perfect outfit for the office: style tips
Choosing the perfect outfit for the office can be challenging, especially if you want to look...
By Dacey Rankins 2024-09-24 13:32:15 0 10K
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov