I created a dictionary using fromkeys and by manually filling it. When I want to update the values of one key, I end up updating all the values when I use the dictionary created by fromkeys. Why is this happening?
ex = dict.fromkeys(list(range(3)), set())
print(ex)
ex[1].update([0,1])
print(ex)
ex2 = {0: set(), 1: set(), 2: set()}
print(ex2)
ex2[1].update([0,1])
print(ex2)
Results
{0: set(), 1: set(), 2: set()}
{0: {0, 1}, 1: {0, 1}, 2: {0, 1}}
{0: set(), 1: set(), 2: set()}
{0: set(), 1: {0, 1}, 2: set()}