The fromkeys()
method in Python is a dictionary method that creates a new dictionary with keys from a specified iterable (such as a list) and values set to a specified value, or None by default if no value is provided.
Parameter Values
Parameter | Description |
---|---|
sequence | An iterable that will be used to create a new dictionary with keys from this iterable and values set to a specified value. |
value | Optional. The value to set for all keys in the newly created dictionary. If not specified, the default value is None. |
Return Values
The fromkeys()
method returns a new dictionary with provided keys and set value.
How to Use fromkeys()
in Python
Example 1:
The fromkeys()
method creates a new dictionary with the specified keys and values.
new_dict = dict.fromkeys(['a', 'b', 'c'], 0)
print(new_dict)