Python Dictionary Key/Value Swap

0
8K

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
Научная фантастика и фэнтези
Безумный Макс: Дорога ярости. Mad Max: Fury Road. (2015)
Преследуемый призраками беспокойного прошлого Макс уверен, что лучший способ выжить - скитаться в...
By Nikolai Pokryshkin 2023-04-05 17:58:27 0 29K
Business
What Happens If I Don't Reach My Funding Goal?
Crowdfunding campaigns have become a popular way to raise money for a wide range of projects,...
By Dacey Rankins 2025-03-28 14:32:06 0 3K
Business
What Is a Startup Incubator?
A startup incubator is an organization designed to support early-stage companies by helping them...
By Dacey Rankins 2025-04-23 16:53:45 0 5K
Journals
Key Elements of Explorers' Journals
Explorers' journals are an essential part of the history of exploration, providing firsthand...
By Dacey Rankins 2024-12-02 14:10:20 0 4K
Social Issues
Smoking Barrels. (1998)
Eddy persuades his three pals to pool money for a vital poker game against a powerful local...
By Leonard Pokrovski 2023-03-05 18:37:17 0 15K
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov