Python Dictionary Key/Value Swap

0
13K

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'}
Pesquisar
Categorias
Leia Mais
Wrestling
The Thrill and Tradition of Sports Wrestling
Sports wrestling, a centuries-old discipline, continues to captivate athletes and audiences alike...
Por Dacey Rankins 2024-07-15 16:41:11 0 17K
Systems
Computers - How they have come to be, and what they are
Computer (English computer, IPA: [kəmˈpjuː.tə(ɹ)]- "calculator", from the Latin computare - to...
Por FWhoop Xelqua 2023-02-16 15:33:57 0 23K
Business
What Strategies Can Solopreneurs Use to Market Their Business?
Marketing is one of the most critical components of a successful business, yet it can often feel...
Por Dacey Rankins 2025-02-13 16:20:46 0 11K
Marketing and Advertising
How Do I Target the Right Audience for User Acquisition?
Targeting the right audience is the foundation of successful user acquisition. You can have...
Por Dacey Rankins 2026-02-26 16:56:43 0 2K
Life Issues
Finding Nemo. (2003)
After his son is captured in the Great Barrier Reef and taken to Sydney, a timid clownfish sets...
Por Leonard Pokrovski 2023-02-28 15:35:47 0 21K

BigMoney.VIP Powered by Hosting Pokrov