Why are the values getting updated for every key?
d = {0: [0], 1: [0, 1]}
for i in range(2, 4):
d[i] = d[i - 1]
d[i].append(i)
print(d)
Output:
{0: [0], 1: [0, 1, 2, 3], 2: [0, 1, 2, 3], 3: [0, 1, 2, 3]}
I want it like this in the output:
{0: [0], 1: [0, 1], 2: [0, 1, 2], 3: [0, 1, 2, 3]}
Can someone please help me understand why this is happening and the possible solutions? It would be great if you could attach the related Python documentation!