Python Dictionary Key/Value Swap

0
11K

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
The economics of pollution
Key Points Economic production can cause environmental damage. This tradeoff...
Por Mark Lorenzo 2023-07-20 18:29:02 0 13K
Business
What Does a Growth Plan Look Like?
A growth plan is the roadmap that outlines how a business intends to expand, capture new markets,...
Por Dacey Rankins 2025-09-04 13:27:09 0 1K
Magazines and E-zines
Exploring the Evolution of News Consumption: Magazines and E-zines
In an age of rapid digital transformation, the way we consume news has undergone a significant...
Por Dacey Rankins 2024-05-06 16:03:45 0 14K
Business
What is the Creator Economy?
The Creator Economy refers to the ecosystem where individuals create and distribute digital...
Por Dacey Rankins 2025-02-24 15:51:50 0 10K
Социальные проблемы
Крестный отец 2. The Godfather Part II. (1974)
Для дона Корлеоне и его сына не существует моральных преград на пути к достижению целей. Они...
Por Nikolai Pokryshkin 2022-10-29 21:00:07 0 32K

BigMoney.VIP Powered by Hosting Pokrov