Python Dictionary Key/Value Swap

0
13K

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
The most unusual newspapers and magazines in the world
1. Modern Drunkard Magazine - USAThe journal has been published every two months since 1996. Its...
By Dacey Rankins 2024-09-17 21:23:41 0 13K
Economics
What Is Economic Growth?
What Is Economic Growth?GDP Growth, Development, and Productivity Economic growth is one of the...
By Leonard Pokrovski 2026-01-28 19:46:30 0 648
History
Lawrence of Arabia. (1962)
The story of T.E. Lawrence, the English officer who successfully united and led the diverse,...
By Leonard Pokrovski 2023-01-16 21:49:58 0 21K
Marketing and Advertising
What Industries Spend the Most on Advertising?
Advertising drives awareness, engagement, and revenue, but not all industries invest in it...
By Dacey Rankins 2026-01-06 15:32:58 0 1K
Business
What Are the Best Tools for Startup Founders?
Starting a new business can be both exciting and overwhelming. As a startup founder, you'll wear...
By Dacey Rankins 2025-04-04 14:15:30 0 13K

BigMoney.VIP Powered by Hosting Pokrov