title: Understanding creating a 2D list with nested loops
body: <p>How do nested for loops (in this case double for loops) work in creating a 2D list.</p>
  
  <p>For example I would like to have a 2x2 matrix that is initialized with 0 as every element.</p>
  
  <p>I got this:</p>
  
  <pre><code>x = [[0 for i in range(row)] for j in range(col)]
  </code></pre>
  
  <p>where row is defined to be the number of rows in the matrix and col is defined to be the number of columns in the matrix. In this case row = 2 and col = 2.</p>
  
  <p>When we print x:</p>
  
  <pre><code>print(x)
  </code></pre>
  
  <p>we will get:</p>
  
  <pre><code>[[0, 0], [0, 0]]
  </code></pre>
  
  <p>which is what we want.</p>
  
  <p>What is the logic behind this? Is <code>[0 for i in range(row)]</code> saying that, for every element in the range of the specified row number, we are going to assign a 0 to effectively create our first row in the matrix?</p>
  
  <p>Then <code>for j in range(col)</code> is saying we repeat the creation of this list according to the specified column number to effectively create more rows, which end up being columns?</p>
  
  <p>How should I be reading this code snippet from left to right?</p>