Python error: Index out of range
<pre><code>seq_sum = []
for i in range(len(sequence)):
    seq_sum[i] = sequence[i] + inv_sequence[i]
print (seq_sum)
</code></pre>

<p>When I try to run this code it return an error: list assignment index out of range. How can I fix the problem?
sequence and inv_sequence are arrays of integers.</p>

<p>You've declared <code>seq_sum</code> to be an empty list. You then try and index in a position other than <code>0</code> which results in an <code>IndexError</code>.</p>

<p>Expanding a list to make it larger is essentially done with <code>append</code>ing, <code>extend</code>ing or <code>slice</code> assignments. Since you <em>sequentially</em> access elements, <code>seq_num.append</code> is the best way to go about this. </p>

<p>That is:</p>

<pre><code>seq_sum[i] = sequence[i] + inv_sequence[i]
</code></pre>

<p>Should be instead changed to:</p>

<pre><code>seq_sum.append(sequence[i] + inv_sequence[i])
</code></pre>

python python-3.x