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>I'd just like to point out that there is a better way to do it, namely</p>

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

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

<p>which gives   </p>

<pre><code>[21, 32, 34, 38, 43, 54, 65, 65, 76, 98]
=&gt; None
</code></pre>

<p>(compiled on repl.it)</p>

<p>Hope this helps!</p>

python python-3.x insertion-sort