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'}
Pesquisar
Categorias
Leia Mais
Телевидение
Инфо 24 - ТВ прямой эфир.
Телеканал «ИНФО 24» начал свое вещание в 2011 году.В 2022 году получил статус...
Por Nikolai Pokryshkin 2022-11-03 19:53:04 0 28K
Business
Describe a Challenging Situation You've Faced and How You Handled It
Employers Look for Problem-Solving Skills and Resilience One of the most telling questions in an...
Por Dacey Rankins 2025-06-18 14:54:01 0 12K
Reproductive Health
Reproductive Health: Understanding Its Importance for Individuals and Society
Reproductive health is a cornerstone of overall well-being, encompassing a wide range of...
Por Dacey Rankins 2024-11-05 15:52:56 0 9K
Marketing and Advertising
What Is Positioning? (Understanding Brand and Product Positioning)
Introduction: The Art and Science of Perception In marketing, positioning is one of the most...
Por Dacey Rankins 2025-10-23 15:26:19 0 2K
Marketing and Advertising
Is Billboard Advertising Effective?
In an era dominated by smartphones, social media, and data-driven digital advertising, many...
Por Dacey Rankins 2026-01-13 15:23:28 0 1K

BigMoney.VIP Powered by Hosting Pokrov