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
Marketing and Advertising
What Are Common Facebook Advertising Mistakes?
Facebook advertising can be one of the most profitable marketing channels available—but...
Por Dacey Rankins 2026-01-22 19:47:09 0 908
Educação
Painting Schools
We are often approached by students who want to study fine arts and subsequently find a...
Por FWhoop Xelqua 2023-06-26 17:13:34 0 26K
Korfball
Discovering Korfball: The Unique Sport of Equality and Strategy
  Discovering Korfball: The Unique Sport of Equality and Strategy In...
Por Leonard Pokrovski 2024-06-30 15:41:59 0 26K
News and Media
Top 50 health magazines in the world
Rank Outlet Total Visits Location 1 Yo Dona 162,824,091 Madrid, Spain 2...
Por Dacey Rankins 2024-04-22 16:00:30 0 14K
Социальные проблемы
Напоказ. Appearances. (2020)
Эва, героиня фильма «Напоказ», молодая счастливая женщина, мать очаровательного...
Por Nikolai Pokryshkin 2022-10-10 18:15:51 0 30K

BigMoney.VIP Powered by Hosting Pokrov