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>It is a <a href="https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions" rel="nofollow">list comprehension</a>. You can rewrite that as follows:</p>

<pre><code>&gt;&gt;&gt; adjacency_matrix = []
&gt;&gt;&gt; for j in range(max_index + 1):
...     inner_list = []
...     for i in range(max_index + 1):
...         inner_list.append(0)
...     adjacency_matrix.append(inner_list) 
</code></pre>

python list-comprehension