Python Dictionary Key/Value Swap

0
13K

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
Financial Services
Interpreting the aggregate demand/aggregate supply model
Key points The aggregate demand/aggregate supply model is a model that...
By Mark Lorenzo 2023-03-28 19:51:46 0 15K
Issues
Global problems of mankind
Progress moves humanity forward, but it also has a downside – global problems. In this...
By FWhoop Xelqua 2023-01-31 15:52:21 0 21K
Business
What Are the Guidelines for Using Copyrighted Material in My Content?
As a content creator, you may be tempted to use copyrighted material in your videos, blog posts,...
By Dacey Rankins 2025-02-21 15:13:36 0 10K
Business
What are the characteristics of a successful entrepreneur?
Entrepreneurship is challenging, but certain qualities can increase the chances of success....
By Dacey Rankins 2025-03-11 15:41:55 0 8K
Law
The Interplay of Society and Law: Navigating the Complex Dynamics
In the intricate dance between society and law, each step influences the other, shaping the...
By Dacey Rankins 2024-06-10 19:55:49 0 15K

BigMoney.VIP Powered by Hosting Pokrov