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
Financial Services
Supply and its determinants
The law of supply The law of supply states that there is a positive...
Por Mark Lorenzo 2023-02-24 17:17:03 0 13K
Photography
Photography: A Journey Through Time
Photography, an art and science that captures moments in time, has undergone remarkable...
Por Dacey Rankins 2024-10-09 16:56:05 0 17K
Mental Health
Autism Spectrum: Screening
About half of parents of children with ASD notice their child's atypical behaviors by age 18...
Por Kelsey Rodriguez 2023-03-08 15:02:27 0 11K
Entertainment
The World of Entertainment Arts: An Overview of Its Impact and Diversity
The realm of entertainment arts encompasses a broad spectrum of creative disciplines designed to...
Por Dacey Rankins 2024-11-26 15:00:39 0 12K
Business
How Do Ethical Issues Affect Employee Behavior?
Ethical issues in the workplace can significantly influence employee behavior, shaping their...
Por Dacey Rankins 2025-01-31 15:31:39 0 14K

BigMoney.VIP Powered by Hosting Pokrov