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 Automate Emails in Microsoft Outlook?
Email is one of the most time-consuming tasks in any office environment. From responding to...
By Dacey Rankins 2026-04-23 17:38:06 0 3K
Productivity
Is it really 21 days to form a habit?
Is It Really 21 Days to Form a Habit? The “21 days” idea is one of the most...
By Michael Pokrovski 2026-05-18 20:09:47 0 1K
Gymnastics
The Graceful Artistry of Gymnastics: A Sport of Power, Precision, and Poise
The Graceful Artistry of Gymnastics: A Sport of Power, Precision, and Poise Gymnastics, often...
By Leonard Pokrovski 2024-06-27 18:32:34 0 25K
Научная фантастика и фэнтези
Метрополис. Metropolis. (1927)
Метрополис - город будущего, разделен на две части. Под землей находятся жилища рабочих, над ними...
By Nikolai Pokryshkin 2023-01-28 17:51:11 1 28K
Productivity
How to avoid skipping habits?
How to Avoid Skipping Habits? Most people think skipping habits is a motivation problem. They...
By Michael Pokrovski 2026-05-18 20:03:54 0 1K

BigMoney.VIP Powered by Hosting Pokrov