1

.remove() has some behavior I don't understand.

x = [[1,2,3]]*3
x[0].remove(1)

then x becomes [[2, 3], [2, 3], [2, 3]].

Why does this happen, and how can I fix this? I want 1 removed from the first list only.

qwr
  • 9,525
  • 5
  • 58
  • 102

1 Answers1

4

If you created x something like this:

a = [1,2,3]
x = [a,a,a]

Then the elements of x are actually the same object, and changing one of them causes the change to be reflected in all of them.

mishik
  • 9,973
  • 9
  • 45
  • 67
ooga
  • 15,423
  • 2
  • 20
  • 21