The dict method {}.fromkeys()
takes an iterable of strings, which will be transformed into dictionary keys. The default value will default to None
. For instance:
d = {}.fromkeys(['loss', 'val_loss'])
{'loss': None, 'val_loss': None}
An optional, second argument is the default value that all these values will take:
d = {}.fromkeys(['loss', 'val_loss'], [])
{'loss': [], 'val_loss': []}
I understand that you can initialize an empty dict like this. Where it gets more complicated is that the default values point to the same object.
[id(val) for val in d.values()]
[140711273848032, 140711273848032]
If you would append one list, it would append all the values of this dict. Therefore, you need to re-assign the values to a new object in order to treat the dict values as separate objects:
d['loss'] = list([0.2, 0.4])
If you don't do that, you will change all dict values at the same time.
My question: what purpose is the optional default value intended to serve?
As I see it, you will need to reassign this default value anyway, unless you want to manipulate the same object with different dict keys. I can't think of an honest reason to have many dict keys for one object.