The dict.fromkeys Method in Python: Creating a Dictionary

Create a dictionary with keys from seq and a value of value (default is None).

dict.fromkeys(seq[, value])

  • seq: An iterable used as the dictionary’s keys.
  • value: The value set for each key. Defaults to None.
keys = ['a', 'b', 'c']
my_dict = dict.fromkeys(keys)  # {'a': None, 'b': None, 'c': None}
default_value_dict = dict.fromkeys(keys, 0)  # {'a': 0, 'b': 0, 'c': 0}