Insertion-sort error in Python
<p>I am trying to make an insertion sorting program that sorted a list in ascending order. I'm getting the error:</p>

<pre><code>line 16, in &lt;module&gt; if listNums[x] &lt; listNums[i]:
</code></pre>

<p>For this program: </p>

<pre><code>    listNums = [54, 21, 76, 43, 65, 98, 65, 32, 34, 38]

    x = 1
    i = 0
    Copy = 0

    for z in range (0, len(listNums)):
           if listNums[x] &lt; listNums[i]:
                  Copy = listNums[i]
                  listNums[i] = listNums[x]
                  listNums[x] = Copy
           i = i + 1
           x = x + 1

    print(listNums)
</code></pre>

<p>This is not an <code>insertion sort error</code> 
In the last iteration i.e. len(listNums)th iteration, the value of x goes beyond the length of list that's why it gives <code>IndexError: list index out of range</code> </p>

python python-3.x insertion-sort