Python Dictionary Key/Value Swap

0
9K

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
Social Issues
Citizenfour (2014)
A documentarian and a reporter travel to Hong Kong for the first of many meetings with Edward...
By Leonard Pokrovski 2023-07-22 17:12:43 0 22K
Multi-Sports
Embracing the Benefits of Multi-Sport Participation
In the world of athletics, the concept of specializing in a single sport from a young age has...
By Dacey Rankins 2024-07-02 17:05:00 0 14K
Newspapers
10 MOST WIDELY READ NEWSPAPERS IN THE WORLD
Over time, it becomes important for newspaper owners to publish their contents in accordance with...
By FWhoop Xelqua 2022-11-02 13:33:41 0 31K
Living History
Living History Recreation: Bringing the Past to Life Through Timeless Experience
Living History Recreation: Bringing the Past to Life Through Timeless Experience In the vast...
By Leonard Pokrovski 2024-05-14 23:29:07 0 15K
Business
What Are the Startup Costs and How Can I Finance My Business?
Starting a business is an exciting journey, but it comes with financial challenges that need...
By Dacey Rankins 2025-02-06 15:12:19 0 10K
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov