0

I'm trying to make a 2d array using lists. From input I have a tuple with the specs of the lines and the columns. Something like this.

((1, 2, 3, 4, 5), (9, 8, 7, 6, 5))

This means that i need a list like this:

[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

(A 5x5 matrix)

to make the matrix, since i don't know the lenth of the specs tuple. I made a constructor to make the matrix. Something like this: (t is the specs tuple)

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

This makes exacly what I want, but when I try to change a value this happens: (y is the matrix)

>>>y[0][1]=2
>>> y
[[0, 2, 0, 0, 0], [0, 2, 0, 0, 0], [0, 2, 0, 0, 0], [0, 2, 0, 0, 0], [0, 2, 0, 0, 0]]

It changes all the [1] index of all the lines. Why is it doing this?

Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57

0 Answers0