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
E-Books
The Rise and Impact of E-books: Revolutionizing Reading in the Digital Age
In the digital age, e-books have transformed the way people read and interact with literature....
By Dacey Rankins 2024-11-20 16:29:33 0 10K
Mental Health
ADHD: Diagnosis
ADHD is diagnosed by an assessment of a person's behavioural and mental development, including...
By Kelsey Rodriguez 2023-04-19 17:32:00 0 14K
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 18K
Business
How Will I Balance Work and Family Life?
One of the most significant challenges when operating a home-based business is balancing the...
By Dacey Rankins 2025-02-04 16:15:05 0 26K
Business
How Is Mentoring Different from Coaching?
Mentoring and coaching are both powerful development tools—but they serve different...
By Dacey Rankins 2025-07-18 17:33:08 0 4K

BigMoney.VIP Powered by Hosting Pokrov