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'}
Zoeken
Categorieën
Read More
Television
The Evolution of Television: A Cultural Revolution in Pictures and Sound
Television has become one of the most influential inventions in modern history, shaping not only...
By Dacey Rankins 2024-11-05 15:40:04 0 11K
Personal Finance
Am I on Track for Retirement?
Am I on Track for Retirement? How Much Do I Need, Is My Savings Enough, and When Can I Retire?...
By Leonard Pokrovski 2025-11-04 22:24:43 0 5K
Financial Services
Indexing and its limitations
Key points A payment is said to be indexed if it is automatically...
By Mark Lorenzo 2023-06-12 20:20:02 0 12K
Boxing
The Art and Sport of Boxing: A Brief Overview
Boxing, often referred to as the "sweet science," is one of the oldest and most celebrated combat...
By Dacey Rankins 2024-11-07 16:28:32 0 12K
Financial Services
Types of market-oriented environmental tools
Key points The three main categories of market-oriented environmental policies are...
By Mark Lorenzo 2023-02-07 17:15:56 0 12K

BigMoney.VIP Powered by Hosting Pokrov