My code of creating a nested dict from 'keys' and then updating one of the elements:
keys = ["a", "b", "c"]
d = dict.fromkeys(keys, {'foo':0, 'bar':[]})
d["a"]["bar"].append("x")
print(d)
I would expect that the resulting dict be the following (only updating 'bar' under key 'a'):
{
"a": {
"foo": 0,
"bar": ["x"]
},
"b": {
"foo": 0,
"bar": []
},
"c": {
"foo": 0,
"bar": []
}
}
But instead I'm getting
{
"a": {
"foo": 0,
"bar": ["x"]
},
"b": {
"foo": 0,
"bar": ["x"]
},
"c": {
"foo": 0,
"bar": ["x"]
}
}