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'}
Buscar
Categorías
Read More
Social Issues
Casino. (1995)
A tale of greed, deception, money, power, and murder occur between two best friends: a mafia...
By Leonard Pokrovski 2023-02-11 19:54:34 0 26K
Business
Why Do Organizations Implement Mentoring Programs?
In today’s dynamic workplace, companies are constantly looking for ways to strengthen...
By Dacey Rankins 2025-07-09 14:36:47 0 6K
Business
What Are the Benefits and Challenges of Remote Work?
Remote work has become increasingly popular, especially in the wake of the global pandemic. Many...
By Dacey Rankins 2025-01-29 17:02:10 0 10K
Major Companies
Major Companies: Pillars of the Global Economy
In the interconnected world of the 21st century, certain companies have emerged as titans,...
By Dacey Rankins 2024-11-15 15:00:01 0 14K
Rodeo
The Thrilling World of Rodeo: Where Grit and Glory Meet
In the heartland of America, where vast expanses of prairie stretch as far as the eye can see,...
By Dacey Rankins 2024-07-05 16:58:55 0 19K

BigMoney.VIP Powered by Hosting Pokrov