title: Python multiple dimension arrays
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/2397141/how-to-initialize-a-two-dimensional-array-in-python">How to initialize a two-dimensional array in Python?</a>  </p>
  </blockquote>
  
  
  
  <p>In solving a simple problem regarding a two dimensional array I came across a solution on this site that explained how to declare one in Python using the overload operator.</p>
  
  <p>Example:</p>
  
  <pre><code>Myarray = [[0]*3]*3
  </code></pre>
  
  <p>this would produce the following array (list)</p>
  
  <pre><code>[[0,0,0],[0,0,0],[0,0,0]]
  </code></pre>
  
  <p>This seems fine until you use it:</p>
  
  <p>if you assign an element for example:</p>
  
  <pre><code>Myarray [0][0] = 1
  </code></pre>
  
  <p>you get the unexpected output:</p>
  
  <pre><code>[[1,0, 0],[1,0,0] , [1,0,0]]
  </code></pre>
  
  <p>In effect assigning Myarray [1][0] and Myarray[2][0] at the same time</p>
  
  <p>My solution:</p>
  
  <pre><code>Myarray = [[][][]]
  for i in range(0,3):
    for j in range (0,3):
       Myarray[i].append(0)
  </code></pre>
  
  <p>This solution works as intended:</p>
  
  <pre><code>Marray[0][1] = 1
  </code></pre>
  
  <p>gives you </p>
  
  <pre><code>[[1,0, 0],[0,0,0] , [0,0,0]]
  </code></pre>
  
  <p>Is there <strong><em>a simpler way</em></strong> to do this? This was a solution to an A level Cambridge question and seems too long winded for students compared to other languages.</p>