Python Dictionary fromkeys() Method
Returns a dictionary from a list of keys and sets them all to the specified
value. If the value is omitted, the value all keys are set to is None
.
Syntax
Python
Copy Code
dictionary.fromkeys(sequence) dictionary.fromkeys(sequence, values)
Parameters
Parameter | Description |
---|---|
sequence |
Required. A sequence of values that will be used as the keys for the dictionary. |
values |
Optional. The values to assign to each key in the new dictionary. |
Example
Python
Copy Code
fish = {'guppy', 'zebra', 'betta'} fishStock = dict.fromkeys(fish, 10) print(fishStock)
Output
{'zebra': 10, 'betta': 10, 'guppy': 10}