def count_inversions(list, inversion_count = 0):
  """
  recursively counts inversions of halved lists
  where inversions are instances where a larger el occurs before a smaller el
  merges the halved lists and increments the inversion count at each level

  :param list list: list containing comparable elements
  :param list list: list containing comparable elements
  :returns: tuple w merged list and number of inversions
  """
  if len(list) < 2:
    return (list, inversion_count)
  mid_point = len(list) / 2
  # recursively count inversions in 1st half of input
  first_half = count_inversions(list[0:mid_point], inversion_count)
  # recursively count inversions in 2nd half of input
  second_half = count_inversions(list[mid_point:len(list)], inversion_count)

  # TODO: indexing into the returned tuple is confusing
  # consider returning a dict instead
  running_inversion_count = first_half[1] + second_half[1]
  return merge_and_count_inversions(first_half[0], second_half[0], running_inversion_count)

def merge_and_count_inversions(a, b, inversion_count):
  """
  steps through indexes in both input lists, appending the smaller val to the merged list at each step
  increments the inversion count when els from list b are appended to the output before a is exhausted

  :param list a: ordered list
  :param list b: ordered list
  :returns: tuple w merged list and number of inversions
  """
  i = 0
  j = 0
  total_len = len(a) + len(b)
  merged = []
  for k in range(total_len):
    try:
      a[i]
    except IndexError:
      # concat merged w remainder of b if a's finished
      merged = merged + b[j:len(b)]
      j += 1
      return (merged, inversion_count)

    try:
      b[j]
    except IndexError:
      # concat merged w remainder of a if b's finished
      merged = merged + a[i:len(a)]
      i += 1
      return (merged, inversion_count)

    if a[i] < b[j]:
      merged.append(a[i])
      i += 1
    else:
      merged.append(b[j])
      j += 1
      # increment inversion_count by num els remaining in a if a isn't exhausted
      try:
        a[i]
        # inversion_count = len(a) - i
        remaining_in_a = len(a) - i
        inversion_count = inversion_count + remaining_in_a
      except IndexError:
        pass # a is exhausted

  return (merged, inversion_count)

list = [ 1, 2, 9, -1, 0]
print count_inversions(list)[1]

# a = [1, 3, 5, 6]
# b = [2, 4, 7, 8, 9]
# print merge_and_count_inversions(a, b)
@@ -366,39 +366,46 @@
 nts%0A  :returns: 
-tuple w
+dict w list, a
  merged list and
@@ -392,37 +392,49 @@
 t, a merged list
+,
  and 
+count, the 
 number of invers
@@ -475,22 +475,40 @@
  return 
-(list,
+%7B %22list%22: list, %22count%22:
  inversi
@@ -515,17 +515,18 @@
 on_count
-)
+ %7D
 %0A  mid_p
@@ -804,102 +804,8 @@
 t)%0A%0A
-  # TODO: indexing into the returned tuple is confusing%0A  # consider returning a dict instead%0A
   ru
@@ -839,17 +839,23 @@
 st_half%5B
-1
+%22count%22
 %5D + seco
@@ -862,17 +862,23 @@
 nd_half%5B
-1
+%22count%22
 %5D%0A  retu
@@ -918,17 +918,22 @@
 st_half%5B
-0
+%22list%22
 %5D, secon
@@ -939,17 +939,22 @@
 nd_half%5B
-0
+%22list%22
 %5D, runni
@@ -1318,15 +1318,22 @@
 ns: 
-tuple w
+dict w list, a
  mer
@@ -1340,21 +1340,33 @@
 ged list
+,
  and 
+count, the 
 number o
@@ -1629,32 +1629,50 @@
      return 
-(merged,
+%7B %22list%22: merged, %22count%22:
  inversion_c
@@ -1671,25 +1671,26 @@
 ersion_count
-)
+ %7D
 %0A%0A    try:%0A 
@@ -1835,32 +1835,50 @@
      return 
-(merged,
+%7B %22list%22: merged, %22count%22:
  inversion_c
@@ -1877,25 +1877,26 @@
 ersion_count
-)
+ %7D
 %0A%0A    if a%5Bi
@@ -2301,16 +2301,34 @@
 urn 
-(merged,
+%7B %22list%22: merged, %22count%22:
  inv
@@ -2339,17 +2339,18 @@
 on_count
-)
+ %7D
 %0A%0Alist =
@@ -2397,17 +2397,23 @@
 s(list)%5B
-1
+%22count%22
 %5D%0A%0A# a =

Return dict instead of tuple for readability