Value assignment leaking over keys when using a dictionary of dictionaries.
Used the fromkeys() function to create the outer dictionary.
Environment: Python 3.6.3
sample_dict = dict.fromkeys(
[1,0],
{}
)
sample_dict[1]['a'] = 1
print(sample_dict)
Output:
{1: {'a': 1}, 0: {'a': 1}}
As you can see, the value gets duplicated for key 0 even when I am only assigning it to key 1.
I tried this and it worked as expected:
sample_dict = dict.fromkeys(
[1,0]
)
sample_dict[1] = {}
sample_dict[1]['a'] = 1
print(sample_dict)
Output:
{1: {'a': 1}, 0: {}}
Is it wrong to nest dictionaries when using the fromkeys() function?