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'}
Site içinde arama yapın
Kategoriler
Read More
Научная фантастика и фэнтези
Бэтмен: Начало. Batman Begins. (2005)
В детстве юный наследник огромного состояния Брюс Уэйн оказался свидетелем убийства своих...
By Nikolai Pokryshkin 2023-02-02 16:23:50 0 35K
Business
How Do I Prepare for a Speech?
Preparing for a speech is one of the most important steps in public speaking. Great speakers...
By Dacey Rankins 2025-12-10 17:46:36 0 1K
Agriculture
Wheat
Harvesting is the process of gathering a ripe crop from the fields. Reaping is the cutting of...
By FWhoop Xelqua 2023-04-21 17:06:13 0 15K
Business
Inspirational Checklist: 25 Things to Try If You Want to Change Your Life
  The list was compiled by psychologists, coaches and personal growth...
By Leonard Pokrovski 2024-07-16 17:38:34 0 19K
Financial Services
Aggregate demand in Keynesian analysis
Key points Aggregate demand is the sum of four components: consumption,...
By Mark Lorenzo 2023-03-15 17:18:10 0 19K

BigMoney.VIP Powered by Hosting Pokrov