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'}
Buscar
Categorías
Read More
Encyclopedias
How to choose a children's encyclopedia
Every day, children ask many different questions: "why?", "why?" and "how?". Little...
By FWhoop Xelqua 2022-09-11 16:20:51 0 35K
Business
Why is a Business Plan Important?
A business plan is more than just a formal document; it's the blueprint for a company's growth,...
By Dacey Rankins 2024-12-18 14:26:23 0 11K
Искусство, культура и развлечения
Аннетт. Annette. (2021)
Действие развернётся в Лос-Анджелесе. По сюжету Генри работает стендап-комиком, а его супруга Энн...
By Nikolai Pokryshkin 2022-09-03 21:19:35 0 51K
Television
SMNI News (English) Live TV Philippines
Sonshine Media Network International (SMNI), is the official broadcasting arm of the Kingdom of...
By Nikolai Pokryshkin 2022-08-30 21:56:36 0 35K
Business
How Can Someone Become a CEO?
Becoming a Chief Executive Officer (CEO) is a career goal that many professionals aspire to...
By Dacey Rankins 2025-06-16 14:54:59 0 9K

BigMoney.VIP Powered by Hosting Pokrov