Python-Wörterbuch fromkeys()- Methode
Beispiel
Erstellen Sie ein Wörterbuch mit 3 Schlüsseln, alle mit dem Wert 0:
x = ('key1', 'key2', 'key3')
y = 0
thisdict = dict.fromkeys(x, y)
print(thisdict)
Definition und Verwendung
Die fromkeys()
Methode gibt ein Wörterbuch mit den angegebenen Schlüsseln und dem angegebenen Wert zurück.
Syntax
dict.fromkeys(keys, value)
Parameterwerte
Parameter | Description |
---|---|
keys | Required. An iterable specifying the keys of the new dictionary |
value | Optional. The value for all keys. Default value is None |
Mehr Beispiele
Beispiel
Gleiches Beispiel wie oben, aber ohne Angabe des Wertes:
x = ('key1', 'key2', 'key3')
thisdict = dict.fromkeys(x)
print(thisdict)