Python Dictionary Key/Value Swap

0
13KB

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'}
Rechercher
Catégories
Lire la suite
Gardening
The Joy of Home Gardening: Cultivating Your Green Thumb
Home gardening has gained immense popularity in recent years, and for good reason. Whether you...
Par Dacey Rankins 2024-10-09 17:39:16 0 19KB
Marketing and Advertising
What Is a Good Cost Per Click (CPC)?
Cost per click (CPC) is one of the most critical metrics in pay-per-click (PPC) advertising. It...
Par Dacey Rankins 2026-02-04 17:49:19 0 5KB
Marketing and Advertising
What Is David Ogilvy’s Biography?
Few names in advertising carry as much weight as David Ogilvy. Often referred to as the father of...
Par Dacey Rankins 2026-01-07 14:24:17 0 6KB
Economics
What are the types of economic policy?
Economic policy refers to the actions, strategies, and decisions governments use to influence a...
Par Leonard Pokrovski 2026-04-16 17:25:32 0 1KB
Социальные проблемы
Жизнь других. The Lives of Others. (2006)
Сто тысяч сотрудников, двести тысяч информаторов. Вместе они — щит и меч партии, знаменитое...
Par Nikolai Pokryshkin 2022-12-15 11:29:22 0 34KB

BigMoney.VIP Powered by Hosting Pokrov