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'}
البحث
الأقسام
إقرأ المزيد
Life Issues
The Gold Rush. (1925)
A prospector goes to the Klondike during the 1890s gold rush in hopes of making his fortune, and...
بواسطة Leonard Pokrovski 2023-03-19 18:45:00 0 21كيلو بايت
Financial Services
Price elasticity of demand and price elasticity of supply
Key points Price elasticity measures the responsiveness of the quantity...
بواسطة Mark Lorenzo 2023-07-04 19:47:29 0 11كيلو بايت
Телевидение
Прямая трансляция телеканала Москва 24
Городской круглосуточный информационный телеканал «Москва 24». Городские новости в...
بواسطة Nikolai Pokryshkin 2022-10-10 11:27:59 0 28كيلو بايت
Business
What are the Primary Responsibilities of a COO?
The Chief Operating Officer (COO) is one of the most critical leadership roles within an...
بواسطة Dacey Rankins 2025-01-28 15:30:12 0 15كيلو بايت
Business
Top 10 economies in terms of GDP by 2038
Top 10 economies in terms of GDP by 2038 In 2024, global...
بواسطة Leonard Pokrovski 2024-07-24 12:41:02 0 17كيلو بايت

BigMoney.VIP Powered by Hosting Pokrov