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
E-Books
E-book (device)
E-book (device) Electric book (reader, coll. "e-reader"; An e-book...
By Leonard Pokrovski 2024-03-21 20:31:42 0 23K
Business
What Is Mentoring?
Mentoring is a powerful, time-tested approach to personal and professional development. At its...
By Dacey Rankins 2025-07-18 17:25:02 0 4K
Financial Services
Elasticity and tax revenue
Key points Tax incidence is the manner in which the tax burden is divided...
By Mark Lorenzo 2023-02-28 15:42:51 0 17K
Central America
Central America Murders
On January 11, 2017, a major event occurred in El Salvador: no one was killed. The news of a day...
By FWhoop Xelqua 2023-04-12 18:10:29 0 24K
Aerospace and Defense
Aviation Industry
Aircraft construction is one of the most profitable and at the same time the most...
By FWhoop Xelqua 2023-07-16 17:12:17 0 23K

BigMoney.VIP Powered by Hosting Pokrov