How to create a list of n dictionaries
<p>I'm learning Python and for practicing purposes I'm writing a script that reads a file (containing a graph in <a href="http://en.wikipedia.org/wiki/Trivial_Graph_Format" rel="nofollow">Trivial Graph Format</a>) and runs a couple of graph algorithms on the graph.</p>

<p>I thought about storing the graph in a list of n dictionaries, where n is the number of vertexes and all the edges of a vertex would be stored in a dictionary.</p>

<p>I tried this</p>

<pre><code>edges = [{} for i in xrange(num_vertexes)]
for line in file:
    args = line.split(' ')
    vertex1 = int(args[0])
    vertex2 = int(args[1])
    label = int(args[2])
    edges[vertex1][vertex2] = label
</code></pre>

<p>but I'm getting this error for the last line:</p>

<blockquote>
  <p>IndexError: list index out of range</p>
</blockquote>
