Python Dictionary fromkeys() Method
Instance
Create a dictionary with 3 keys, all values are 0:
x = ('key1', 'key2', 'key3') y = 0 thisdict = dict.fromkeys(x, y) print(thisdict)
Definition and Usage
The fromkeys() method of dict returns a dictionary with specified keys and values.
Syntax
dict.fromkeys(keys, value)
Parameter Values
Parameters | Description |
---|---|
keys | Required. An iterable object specifying the new dictionary keys. |
value | Optional. All values of the keys. The default value is None. |
More Examples
Instance
As in the above example, but the value is not specified:
x = ('key1', 'key2', 'key3') thisdict = dict.fromkeys(x) print(thisdict)