Python Dictionary Key/Value Swap

0
12KB

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'}
Pesquisar
Categorias
Leia mais
Horror
Se7en. (1995)
Two detectives, a rookie and a veteran, hunt a serial killer who uses the seven deadly sins as...
Por Leonard Pokrovski 2022-11-16 21:08:29 0 22KB
Mental Health
Psychosis: Medication
Administration, or sometimes withdrawal, of a large number of medications may provoke psychotic...
Por Kelsey Rodriguez 2023-06-02 19:57:14 0 11KB
Business
How Do I Develop a Business Strategy?
Developing a business strategy is essential for guiding an organization toward its long-term...
Por Dacey Rankins 2024-12-24 13:06:37 0 18KB
Financial Services
Explicit and implicit costs and accounting and economic profit
Key points Privately owned firms are motivated to earn profits. Profit is the...
Por Mark Lorenzo 2023-02-23 18:34:44 0 15KB
Healthcare
The Importance of Healthcare: A Vital Pillar for Well-Being
Healthcare is a fundamental aspect of society, playing a critical role in maintaining and...
Por Dacey Rankins 2025-01-20 16:23:19 0 12KB

BigMoney.VIP Powered by Hosting Pokrov