title: Counting Inversions Using Merge Sort
body: <p>I have made a merge sort program in Python and it is running perfectly but I have modified it to count the number of inversions involved and now it is giving me an error :</p>
  
  <p>Here's my code:</p>
  
  <pre><code>def merge_list(left,right,c):
      result=[]
      i,j=0,0
      while i &lt; len(left) and j &lt; len(right):
          if left[i] &lt; right[j]:
              result.append(left[i])
              print "Left result",result
              i=i+1
          elif left[i] &gt; right[j]:
              result.append(right[j])
              print "Right result",result
              j=j+1
          if right[j] &lt; left[i]  and i&lt;j:
              c=c+1
      result=result+left[i:]
      result=result+right[j:]
      print "Inversions: ",c
      return result,c
  
  
  def sort_and_count(lis,count):
  
      if len(lis)&lt;2:
          return lis
      middle=len(lis) / 2
      left,c1=sort_and_count(lis[:middle],count)
      print "left",left
      right,c2=sort_and_count(lis[middle:],count)
      print "right",right
      m,c=merge_list(left,right,count)
      c=c+c1+c2
      return m,c
  
  
  if __name__=="__main__":
  
      print "Enter 6 elements: "
      i=0;lis=[];merge_lis=[];inv=0
      while i&lt;=5:
          x=int(raw_input())
          lis.append(x)
          i=i+1
      count=0
      merge_lis,inv=sort_and_count(lis,count)
      print "Sorted list is: ",merge_lis,inv
  </code></pre>
  
  <p>And my traceback:</p>
  
  <pre><code>Traceback (most recent call last):
    File "Sort_and_count.py", line 53, in &lt;module&gt; 
      merge_lis,inv=sort_and_count(lis,count) 
    File "Sort_and_count.py", line 31, in sort_and_count 
      left,c1=sort_and_count(lis[:middle],count) 
    File "Sort_and_count.py", line 31, in sort_and_count
      left,c1=sort_and_count(lis[:middle],count)
  ValueError: need more than 1 value to unpack
  </code></pre>
  
  <p>Where am I going wrong with this approach?</p>