Python Dictionary Key/Value Swap

0
12χλμ.

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'}
Αναζήτηση
Κατηγορίες
Διαβάζω περισσότερα
Business
Selling or Closing a Business: A Guide for Entrepreneurs
Whether you're looking to move on to new opportunities or simply want to exit your business,...
από Dacey Rankins 2024-12-17 14:29:16 0 15χλμ.
Personal Finance
How Much Does Education Cost?
How Much Does Education Cost? Education is often described as an investment in the...
από Leonard Pokrovski 2025-10-30 22:32:04 0 6χλμ.
Science Fiction and Fantasy
The Tomorrow War (2021)
A family man is drafted to fight in a future war where the fate of humanity relies on his ability...
από Leonard Pokrovski 2022-09-09 18:59:28 0 31χλμ.
Социальные проблемы
Не возненавидь. Thou Shalt Not Hate. (2020)
Симоне, хирург еврейского происхождения, становится свидетелем автомобильной аварии. По иронии...
από Nikolai Pokryshkin 2022-10-13 15:49:01 0 23χλμ.
Научная фантастика и фэнтези
Назад в будущее. Back to the Future. (1985)
Подросток Марти с помощью машины времени, сооружённой его другом-профессором доком Брауном,...
από Nikolai Pokryshkin 2022-11-23 16:35:23 0 29χλμ.

BigMoney.VIP Powered by Hosting Pokrov