Python Dictionary Key/Value Swap

0
14K

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
Business
How to Reduce Paper Usage?
The printer wouldn’t stop humming. Page after page slid into the output tray while nobody...
By Dacey Rankins 2026-05-07 00:53:46 0 2K
Life Issues
American Beauty. (1999)
A sexually frustrated suburban father has a mid-life crisis after becoming infatuated with his...
By Leonard Pokrovski 2022-12-24 16:49:59 0 33K
Business and Corporate Finance
What Tools and Software Do CFOs Use?
What Tools and Software Do CFOs Use? ERP Systems, FP&A Tools, Dashboards, and Analytics...
By Leonard Pokrovski 2026-01-14 16:22:08 0 6K
Personal Finance
How Is My Credit Score Calculated?
How Is My Credit Score Calculated? Your credit score is one of the most important numbers in...
By Leonard Pokrovski 2025-10-24 11:16:49 0 8K
Business
How Do I Create a Business Plan for a Startup?
Entrepreneurs seek tips and examples tailored to startups when creating a business plan. A...
By Dacey Rankins 2024-12-18 16:56:58 0 31K

BigMoney.VIP Powered by Hosting Pokrov