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
Personal Finance
Are Pensions and Retirement Benefits Taxable? A Comprehensive Guide
Are Pensions and Retirement Benefits Taxable? A Comprehensive Guide Planning for retirement...
Por Leonard Pokrovski 2025-11-26 20:23:16 0 7K
Business
Drew Houston
Drew Houston Drew Houston Andrew W. "Drew" Houston   Date of...
Por Leonard Pokrovski 2024-08-16 14:54:55 0 25K
Социальные проблемы
Выжившая. Кровь и металл. Spare Parts. (2020)
После не особо удачного концерта участниц панк-группы похищают неизвестные, ампутируют им руки и...
Por Nikolai Pokryshkin 2022-10-16 19:01:18 0 40K
Programming
Mojo Language
Mojo is a Python type language that uses C for the performance. Python is normally slow when it...
Por Jesse Thomas 2023-05-05 20:31:29 0 12K
Personal Finance
How Much Does Education Cost?
How Much Does Education Cost? Education is often described as an investment in the...
Por Leonard Pokrovski 2025-10-30 22:32:04 0 7K

BigMoney.VIP Powered by Hosting Pokrov