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
Социальные проблемы
12 лет рабства. 12 Years a Slave. (2013)
Соломон Нортап был женатым и образованным мужчиной, который жил и работал в штате Нью-Йорк, когда...
Por Nikolai Pokryshkin 2023-03-21 12:18:17 0 44K
Business
What Are Common Mistakes When Giving Feedback?
Feedback is one of the most powerful tools for learning and performance improvement—but...
Por Dacey Rankins 2025-07-24 18:02:20 0 4K
Marketing and Advertising
What Metrics and KPIs Should Be Used to Measure Marketing Success?
Introduction In marketing, success is only as clear as the metrics used to define it. Without...
Por Dacey Rankins 2025-10-16 18:54:11 0 1K
Business
How Can I Validate My Business Idea Without Significant Funding?
Validating a business idea is an essential step in the entrepreneurial process, but many aspiring...
Por Dacey Rankins 2025-03-31 15:51:14 0 13K
Social Issues
A Christmas Gift from Bob (2020)
A struggling street musician finds himself the target of an animal welfare investigation that...
Por Leonard Pokrovski 2022-10-07 22:30:32 0 28K

BigMoney.VIP Powered by Hosting Pokrov