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'}
Buscar
Categorías
Read More
Научная фантастика и фэнтези
Темный рыцарь. The Dark Knight. (2008)
Бэтмен поднимает ставки в войне с криминалом. С помощью лейтенанта Джима Гордона и прокурора...
By Nikolai Pokryshkin 2022-10-28 21:31:58 0 26K
Social Issues
Mercy. (2023)
Leah Gibson (Jessica Jones), Jonathan Rhys Meyers (Vikings) and Academy Award® Winner Jon...
By Leonard Pokrovski 2023-06-13 20:23:05 0 36K
Financial Services
How perfectly competitive firms make output decisions
Key points As a perfectly competitive firm produces a greater quantity of output,...
By Mark Lorenzo 2023-07-19 22:25:39 0 10K
Business
How Does Crowdfunding Work?
Crowdfunding has become a revolutionary way for entrepreneurs, creators, and organizations to...
By Dacey Rankins 2025-03-27 18:07:28 0 5K
Office Products
Office Tools
Choosing office suppliesIs it time to stock up on office stationery? Not sure where to start?...
By FWhoop Xelqua 2023-03-25 16:24:05 0 21K
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov