Python Dictionary Key/Value Swap

0
9K

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'}
Căutare
Categorii
Citeste mai mult
Business
Google Ads contextual advertising: an effective way to attract customers
Contextual advertising in Google Ads (formerly known as Google AdWords) is one of the most...
By Dacey Rankins 2024-09-16 14:25:42 0 13K
Extended Coverage
The next years may be the worst for the world economy, the investor believes
WASHINGTON, Dec. 12 — PRIME. The next two or three years may be the worst for the world...
By FWhoop Xelqua 2022-12-28 11:55:37 0 18K
Motorsports
The Thrill of Motorsports: Speed, Skill, and Spectacle
The Thrill of Motorsports: Speed, Skill, and Spectacle Motorsports, a captivating blend of...
By Leonard Pokrovski 2024-07-01 23:13:01 0 18K
Business
Can a Business Consultant Help with Implementing Their Recommendations?
When a company hires a business consultant, they often expect valuable insights and...
By Dacey Rankins 2025-02-11 15:46:59 0 9K
Science Fiction and Fantasy
Harry Potter 20th Anniversary: Return to Hogwarts (2022)
Cast members from all "Harry Potter" films reunite in a retrospective special to celebrate the...
By Leonard Pokrovski 2023-07-18 22:15:55 0 25K
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov