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
  • 8,081
  • 5
  • 52
  • 86

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,873
  • 9
  • 42
  • 65
ooga
  • 15,118
  • 2
  • 19
  • 21