I feel like I'm either missing something really stupid on this. I have the following code:
graph = [[]] * 10
for i in range(2):
print(graph)
graph[0].append(1)
print(graph)
it outputs
[[], [], [], [], [], [], [], [], [], []]
[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]
[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]
[[1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1]]
Why at each iteration is graph[0].append(1)
updating every sublist in graph
? Shouldn't it just be adding 1 to the first sublist? (graph[0]
). This is happening on Leetcode by the way, not sure if it could be speicifc to that.