Python Dictionary Key/Value Swap

0
10K

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'}
Search
Categories
Read More
Sport
Kill Bill: Vol. 1. (2003)
After awakening from a four-year coma, a former assassin wreaks vengeance on the team of...
By Leonard Pokrovski 2023-02-24 17:06:46 0 18K
Science Fiction and Fantasy
Star Wars: Episode IV - A New Hope. (1977)
Luke Skywalker joins forces with a Jedi Knight, a cocky pilot, a Wookiee and two droids to save...
By Leonard Pokrovski 2022-11-22 10:13:34 0 25K
Entertainment
Exploring Kids and Teens Entertainment: From Screen Time to Creative Play
Exploring Kids and Teens Entertainment: From Screen Time to Creative Play Entertainment plays a...
By Leonard Pokrovski 2024-04-30 15:11:22 0 30K
Business
How Do You Encourage Innovation Within Your Team?
In today’s rapidly changing world, innovation is not a luxury—it’s a necessity....
By Dacey Rankins 2025-05-09 15:03:30 0 5K
Social Issues
L.A. Confidential. (1997)
As corruption grows in 1950s Los Angeles, three policemen - one strait-laced, one brutal, and one...
By Leonard Pokrovski 2023-01-30 14:31:19 0 21K

BigMoney.VIP Powered by Hosting Pokrov