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'}
Pesquisar
Categorias
Leia Mais
Programming
The World of Programming: An In-Depth Exploration
Programming is a fundamental skill in today’s digital landscape, shaping the way we...
Por Dacey Rankins 2024-10-11 15:46:15 0 24K
Life Issues
One Flew Over the Cuckoo's Nest. (1975)
In the Fall of 1963, a Korean War veteran and criminal pleads insanity and is admitted to a...
Por Leonard Pokrovski 2022-11-15 21:26:25 0 26K
Financial Services
Types of market-oriented environmental tools
Key points The three main categories of market-oriented environmental policies are...
Por Mark Lorenzo 2023-02-07 17:15:56 0 12K
Business
When Is the Right Time to Expand Internationally?
For many businesses, expanding internationally represents the ultimate growth milestone. Global...
Por Dacey Rankins 2025-09-05 15:43:13 0 5K
Marketing and Advertising
How Much Time and Resources Does Social Media Marketing Require?
Social media marketing is one of the most powerful tools for building a brand, driving...
Por Dacey Rankins 2025-11-03 14:58:46 0 9K

BigMoney.VIP Powered by Hosting Pokrov