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'}
Search
Categories
Read More
Finance
The Main Types and Segments of Fintech
The Main Types and Segments of Fintech Introduction Financial technology—better known as...
By Leonard Pokrovski 2025-10-09 17:29:32 0 4K
Business
What Is a Pitch?
Introduction: Why “Pitching” Matters Everywhere Whether you're trying to attract...
By Dacey Rankins 2025-12-02 20:07:43 0 2K
Arts, Culture and Entertainment
Exit Through the Gift Shop (2010)
Following the style of some of the world's most prolific street artists, an amateur filmmaker...
By Leonard Pokrovski 2023-07-26 20:20:46 0 37K
Business
How Do I Pitch My Business Idea? A Complete Guide
Pitching a business idea is a critical skill for anyone interested in entrepreneurship,...
By Dacey Rankins 2025-12-02 19:10:41 0 2K
Business
How Do I Write an Elevator Pitch?
Writing an elevator pitch is one of the most powerful communication skills you can learn,...
By Dacey Rankins 2025-12-02 19:01:33 0 1K

BigMoney.VIP Powered by Hosting Pokrov