Python Dictionary Key/Value Swap

0
13كيلو بايت

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'}
البحث
الأقسام
إقرأ المزيد
Programming
Encoding and Decoding Base64 Strings in Python
The Base64 encoding is used to convert bytes that have binary or text data into ASCII characters....
بواسطة Jesse Thomas 2023-03-10 20:59:12 0 14كيلو بايت
Business
Organizing Chart: Hats and What's Underneath?
Do we really need structure... It is good if everything works smoothly in the company: work...
بواسطة Dacey Rankins 2024-09-23 14:40:42 0 15كيلو بايت
Business
What Are the Key Responsibilities of a Startup CEO?
  The role of a startup CEO is both exhilarating and daunting. Unlike in mature companies...
بواسطة Dacey Rankins 2025-05-01 16:21:54 0 10كيلو بايت
История
Паровоз Генерал. The General. (1926)
Юного Джонни Грэя в армию Конфедерации не взяли, а его возлюбленная Аннабел Ли отвергает его,...
بواسطة Nikolai Pokryshkin 2023-03-26 14:24:04 0 27كيلو بايت
Социальные проблемы
История игрушек. Toy Story. (1995)
В спальне Энди его игрушки начинают жить своей жизнью, как только он выходит из комнаты. В день...
بواسطة Nikolai Pokryshkin 2022-12-31 11:10:19 1 28كيلو بايت

BigMoney.VIP Powered by Hosting Pokrov