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
Personal Finance
What Are Tax Advantages?
What Are Tax Advantages? Meaning of Tax Benefits and Tax-Efficient Strategies Taxes are a part...
Por Leonard Pokrovski 2025-12-19 23:11:57 0 763
Телевидение
Прямая трансляция телеканала Ю
«Ю» - молодёжный, женский телеканал. Любовь и романтические отношения, семейная жизнь...
Por Nikolai Pokryshkin 2022-10-01 13:08:51 0 34K
Business
How Do I Close a Sale?
Closing a sale is one of the most important skills in sales. It’s the moment where all your...
Por Dacey Rankins 2025-12-12 19:55:57 0 772
Жизненные вопросы
Земляничная поляна. Wild Strawberries. (1957)
78-летний профессор из Стокгольма вспоминает и пересматривает разочарования своей долгой жизни....
Por Nikolai Pokryshkin 2023-03-26 13:25:00 0 29K
Business
How Does Technology, Particularly AI, Influence Startup Innovation?
In today’s fast-evolving business landscape, technology, especially artificial intelligence...
Por Dacey Rankins 2025-03-21 14:15:44 0 13K

BigMoney.VIP Powered by Hosting Pokrov