Python Dictionary Key/Value Swap

0
9كيلو بايت

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
Build a 2D game with Python
Python is an outstanding language for people learning to program, and perfect for anyone...
بواسطة Jesse Thomas 2023-06-30 21:26:00 0 11كيلو بايت
Health
Children's Health Trends: Insights Backed by Research and Statistics
In recent years, research and statistical analysis have provided invaluable insights into the...
بواسطة Dacey Rankins 2024-04-30 18:30:08 0 15كيلو بايت
Financial Services
Law of supply
Key points The law of supply states that a higher price leads to a higher...
بواسطة Mark Lorenzo 2023-06-28 19:25:37 0 10كيلو بايت
Business
Text of the scientific work on the topic "ANALYSIS OF MODERN APPROACHES TO THE DEFINITION OF INNOVATIVE ACTIVITY OF THE ENTERPRISE"
Text of the scientific work on the topic "ANALYSIS OF MODERN APPROACHES TO THE DEFINITION OF...
بواسطة Leonard Pokrovski 2024-08-22 12:49:36 0 16كيلو بايت
Awards
Film Awards
Oscar Academy Awards, OscarLos Angeles, California, USA Golden Globes Los Angeles, California,...
بواسطة FWhoop Xelqua 2023-06-22 18:26:06 0 34كيلو بايت
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov