Python Dictionary Key/Value Swap

0
13χλμ.

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'}
Αναζήτηση
Κατηγορίες
Διαβάζω περισσότερα
Sport
Creed. (2015)
The former World Heavyweight Champion Rocky Balboa serves as a trainer and mentor to Adonis...
από Leonard Pokrovski 2023-04-29 18:02:07 0 28χλμ.
Financial Services
Elasticity in areas other than price
Key points Elasticity is a general term, referring to percentage change of one...
από Mark Lorenzo 2023-03-01 17:29:34 0 16χλμ.
Business
What Are the Latest Trends in Entrepreneurship?
The world of entrepreneurship is constantly evolving, driven by technological advancements,...
από Dacey Rankins 2025-03-18 13:53:48 0 9χλμ.
Business
How to Create a Viral Newsbreak: Techniques and Examples
Viral newsbreaks help to achieve different KPIs: increase awareness, engagement, and audience...
από Dacey Rankins 2024-09-11 12:01:20 0 11χλμ.
Social Issues
A Christmas Gift from Bob (2020)
A struggling street musician finds himself the target of an animal welfare investigation that...
από Leonard Pokrovski 2022-10-07 22:30:32 0 29χλμ.

BigMoney.VIP Powered by Hosting Pokrov