0

Python doesn't support multidimensional arrays, but I thought this workaround would be easy enough. It's just not working though. Here's my code

mat = [ [ None ] * 3 ] * 3
mat[0][0] = 1
return mat

Output

[[1, None, None], [1, None, None], [1, None, None]]

Why is it changing mat[1][0] and mat[2][0]? If I ask it to print mat[0][0] it just gives me 1 like it should.

  • 1
    The actual related one is - [Python list of lists, changes reflected across sublists unexpectedly](http://stackoverflow.com/questions/240178/python-list-of-lists-changes-reflected-across-sublists-unexpectedly) – Anand S Kumar Sep 30 '15 at 11:59
  • If you want to do a lot with multi-dimensional arrays, then you should look at numpy. – michaelrccurtis Sep 30 '15 at 11:59
  • Thanks @Anand. I couldn't for the life of me find this answer. – Luke Shirley Sep 30 '15 at 12:06
  • You may find this article helpful: [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html), which was written by SO veteran Ned Batchelder. – PM 2Ring Sep 30 '15 at 12:15

0 Answers0