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'}
Site içinde arama yapın
Kategoriler
Read More
Middle East
Egypt
Egypt (Arabic: Yeghipet; مصر Misr/Miṣr [misʕɾ], masri مصر Masr/Maṣr [ˈmɑsʕɾ], Copt. Ⲭⲏⲙⲓ...
By FWhoop Xelqua 2023-04-13 17:08:13 0 19K
Economics
What does comparative economics study?
Comparative economics is a branch of economics that studies how different economic systems are...
By Leonard Pokrovski 2026-03-02 19:57:43 0 3K
Financial Services
How the AD/AS model incorporates growth, unemployment, and inflation
Key points The aggregate demand/aggregate supply, or AD/AS, model is one of the...
By Mark Lorenzo 2023-03-31 19:57:02 0 15K
Business
Employee Motivation: Trends, Science, Practice, Assessment
Employee Motivation: Trends, Science, Practice, AssessmentMaterial and Non-Material Incentives...
By Leonard Pokrovski 2024-08-11 16:05:13 0 22K
Marketing and Advertising
What Is the Difference Between YouTube Ads and TV Ads?
For decades, television advertising dominated brand marketing. A commercial during prime-time...
By Dacey Rankins 2026-03-03 18:05:07 0 1K

BigMoney.VIP Powered by Hosting Pokrov