What does a number mean before a for-loop
<p>I'm new to Python. Would greatly appreciate if you could explain how this line works. What does it mean to have a number before a for-loop?</p>

<pre><code>adjacency_matrix = [[0 for i in range(max_index + 1)] for j in range(max_index + 1)]
</code></pre>

<p>I know that </p>

<pre><code>max_index = 4
adjacency_matrix = [[0 for i in range(max_index + 1)] for j in range(max_index + 1)]
&gt;&gt;&gt;[[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]] 
</code></pre>

<p>But I don't understand how it works. </p>

<p>Thanks</p>

<p>The <code>0</code> is the value which the list comprehension is adding to the array (list) - same way each list of zeros is getting nested in the parent list. Try changing it to a different integer, float, string, boolean, etc...</p>

<pre><code>&gt;&gt;&gt; max_index = 4
&gt;&gt;&gt; adjacency_matrix = adjacency_matrix = [[True for i in range(max_index + 1)] for j in range(max_index + 1)]
&gt;&gt;&gt; adjacency_matrix
[[True, True, True, True, True],
 [True, True, True, True, True],
 [True, True, True, True, True],
 [True, True, True, True, True],
 [True, True, True, True, True]]
</code></pre>

<p>Otherwise, see @ozgur's answer for a translation of the list comprehension into articulated for-loops and a link to the PyDoc for list comps.  </p>

<p>Essentially, tho, what you are invoking is:  </p>

<pre><code>[ ADD_THE_FOLLOWING_LISTS:

  [ADD_THIS_VALUE_TO_EACH_INNER_LIST = 0, True, etc...
   `for` each_integer
   `in` the `range`_of
   (zero through four - not including five)]  = [value, value, value, value, value]

...............................`for` each_number
                               `in` the `range`_of
                               (zero through four - not including five)]  
    ]   =  a (4 + 1) x (4 + 1), or 5x5 grid of values

[ [value, value, value, value, value],
  [value, value, value, value, value],
  [value, value, value, value, value],
  [value, value, value, value, value],
  [value, value, value, value, value] ]
</code></pre>

python list-comprehension