-2

I am trying to initialize a list, but then I have to change each cell in said list. I did:

matrix = [[[0] * len(t[0])] * len(t[0])]

But when trying to change one cell it changes the entire column of the matrix. Why is this?

Martin Evans
  • 43,220
  • 16
  • 78
  • 90
raissa
  • 1
  • len(t[0]) is variable, deppends on user. The only problem is on the list/matrix @DylanLawrence – raissa Dec 03 '15 at 16:06

1 Answers1

1

Yes the problem is that you are copying references and not creating new objects, to solve that you can do something like this:

matrix = [[0 for _ in xrange(len(t[0]))] for _ in range(len(t[0]))]
Alberto Bonsanto
  • 16,537
  • 10
  • 61
  • 91