طريقة قاموس بايثون من المفاتيح ()
مثال
أنشئ قاموسًا بثلاثة مفاتيح ، كل ذلك بالقيمة 0:
x = ('key1', 'key2', 'key3')
y = 0
thisdict = dict.fromkeys(x, y)
print(thisdict)
التعريف والاستخدام
تقوم fromkeys()
الطريقة بإرجاع قاموس بالمفاتيح المحددة والقيمة المحددة.
بناء الجملة
dict.fromkeys(keys, value)
قيمه المعامل
Parameter | Description |
---|---|
keys | Required. An iterable specifying the keys of the new dictionary |
value | Optional. The value for all keys. Default value is None |
مزيد من الأمثلة
مثال
نفس المثال أعلاه ، ولكن بدون تحديد القيمة:
x = ('key1', 'key2', 'key3')
thisdict = dict.fromkeys(x)
print(thisdict)