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'}
Cerca
Categorie
Leggi tutto
Africa
How Do they celebrate the new years in Africa?
Connection between the calendar and natural phenomena, new Year's rituals of Africans and the...
By FWhoop Xelqua 2022-12-30 15:18:01 0 24K
Personal Finance
When Is Debt “Bad” vs. “Good”?
When Is Debt “Bad” vs. “Good”? Debt often carries a negative reputation...
By Leonard Pokrovski 2025-10-28 16:30:53 0 3K
Financial Services
The expenditure-output, or Keynesian cross, model
Key points The expenditure-output model, or Keynesian cross diagram, shows how the...
By Mark Lorenzo 2023-03-10 20:43:28 0 15K
Productivity
How can I clean faster and easier?
How Can I Clean Faster and Easier? Cleaning is one of those necessary tasks that most people...
By Michael Pokrovski 2026-04-02 00:14:22 0 2K
Business
How Do I Tailor Feedback for Bosses, Peers, and Reports?
Giving feedback isn’t one-size-fits-all. How you deliver feedback should vary depending on...
By Dacey Rankins 2025-07-29 17:38:25 0 4K

BigMoney.VIP Powered by Hosting Pokrov