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
Money
How Much Do I Need for Retirement?
How Much Do I Need for Retirement? Retirement planning is one of the most important financial...
Por Leonard Pokrovski 2025-09-30 22:50:17 0 7K
Personal Organization
Social work
Social work with the elderly in the UNITED STATES In the United States, the implementation of...
Por FWhoop Xelqua 2023-01-21 17:55:34 0 24K
Financial Services
How to establish an emergency savings fund
Establishing an emergency savings fund now can have a big payoff later. Setting aside emergency...
Por Mark Lorenzo 2023-05-17 20:13:55 0 18K
Financial Services
The expenditure-output, or Keynesian cross, model
Key points The expenditure-output model, or Keynesian cross diagram, shows how the...
Por Mark Lorenzo 2023-03-10 20:43:28 0 15K
Business
Why Are Communication Skills Important?
Introduction Communication skills are among the most fundamental competencies that influence...
Por Dacey Rankins 2025-11-20 15:10:11 0 2K

BigMoney.VIP Powered by Hosting Pokrov