Index Error : list index out of range in python code
<p>This is my Python code for merge sort, and I can't understand why I am getting an <code>IndexError</code> on line 23 .</p>

<p>line 23 does not give an error if put before the <code>for</code> loop but in the <code>for</code> loop it says <code>list index out of range</code>. :(</p>

<pre><code>from math import floor

def merge_sort(a,p,r):
 if p &lt; r:
  q = (p+r)/2
  merge_sort(a,p,q)
  merge_sort(a,q+1,r)
  merge(a,p,q,r)

def merge(A,p,q,r):
 n1 = q - r +1
 n2 = r - q
 L = []
 R = []
 #print n1
 for i in range (1,n1):
  L.append(a[p+i-1])
 for j in range (1,n2):
  R.append(a[q+j])
 L.append(10000)
 R.append(10000)
 i,j=0,0

 for k in range (p,r):
  if L[i] &lt;= R [j]: # This is where the error occurs
   A[k] = L[i]
   i = i + 1
  else :  
   A[k] = R[j]
   j = j + 1



a=[1,4,9,8,2,3,8,2,9]
merge_sort(a,1,len(a)) 
print a
</code></pre>
