Appending lists according to a pair of lists (IndexError: list index out of range)
<p>I have these lists:</p>

<pre><code>n_crit = [[1, 2, 3, 4, 5, 6], [1, 1, 1, 1, 1, 1], [-1, 1, -1, -1, -1, 1], [2, 3, 5, 4, 1, 6], [10, 0, 0.5, 1, 0, 0], [0, 30, 5, 6, 0, 0], [0, 0, 0, 0, 0, 5]]

crit = [[80, 90, 6, 5.4, 8, 5], [65, 58, 2, 9.7, 1, 1], [83, 60, 4, 7.2, 4, 7], [40, 80, 10, 7.5, 7, 10], [52, 72, 6, 2, 3, 8], [94, 96, 7, 3.6, 5, 6]]
</code></pre>

<p>and i have these code:</p>

<pre><code>DivMatrix = []
for x in range(len(crit)):
    subList1 = []
    for y in range(len(crit[x])):
        subList2 = []
        if (n_crit[2][x]&gt;0):
            for z in range(len(crit[x])):
                subList2.append(crit[y][x] - crit[z][x])
        elif (n_crit[2][x]&lt;0):
            for z in range(len(crit[x])):
                subList2.append(-(crit[y][x] - crit[z][x]))   
        subList1.append(subList2)
    DivMatrix.append(subList1)    
</code></pre>

<p>Now I want to use the same code for another pair of lists that are:</p>

<pre><code>n_crit = [[1, 2, 3, 4, 5], [0.23, 0.15, 0.15, 0.215, 0.255], [-1, -1, 1, 1, 1], [2, 6, 5, 4, 1], [4000, 0, 20, 0, 0], [0, 0, 40, 2, 0], [0, 1.5, 0, 0, 0]]

crit = [[15000, 7, 60, 3, 3], [27000, 9, 120, 7, 7], [19000, 8.5, 90, 4, 5], [36000, 10, 140, 8, 7]]
</code></pre>

<p>But instead I get this error message:</p>

<pre><code>    subList2.append(-(crit[y][x] - crit[z][x]))
IndexError: list index out of range
</code></pre>

<p>I really don't know what is wrong but I want to use this code for any pair of lists I want.</p>

<p>At the time the exception is raised your Z value 4 but the n_crit is a list of 4 so index 4 (5th in the list) doesn't exist </p>

python list for-loop append