Python Dictionary Key/Value Swap

0
9Кб

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'}
Поиск
Категории
Больше
Horror
Old (2021)
A vacationing family discovers that the secluded beach where they're relaxing for a few hours is...
От Leonard Pokrovski 2022-09-08 19:07:15 0 26Кб
История
Апокалипсис сегодня. Apocalypse Now. (1979)
Во время Вьетнамской войны капитан Уиллард отправляется вверх по реке в Камбоджу со спецзаданием:...
От Nikolai Pokryshkin 2022-12-11 18:22:10 0 26Кб
Mental Health
ADHD: Pathophysiology
Current models of ADHD suggest that it is associated with functional impairments in some of the...
От Kelsey Rodriguez 2023-04-10 18:57:55 0 8Кб
Business
How to manage subscriptions and unsubscribes to on-site and off-site newsletters
There are 2 pieces of news: bad and good. The bad news is that your subscriber base is degraded...
От Dacey Rankins 2024-09-27 15:16:00 0 25Кб
Programming
Proxy Webserver in Python
Socket programming in python is very user friendly as compared to c. The programmer need not...
От Jesse Thomas 2023-03-13 22:09:22 0 8Кб
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov