title: Median of a List in Python
body: <p>i've looked up similar posts to these and the examples people have posted are throwing the same error at me just as my own version is. I keep getting the error "list indices must be integers, not float." I believe my logic behind getting the median is fine but I can't figure out how to get around this. I understand that this is happening because 5/2 = 2.5 and that isn't a valid index, but how am I supposed to get the median of an even list in that case?</p>
  
  <p>My short code is:</p>
  
  <pre><code>def median(lst):
  
      lst.sort()
  
      a = len(lst)
  
      if a % 2 == 0:
          b = lst[len(lst)/2]
          c = lst[(len(lst)/2)-1]
          d = (b + c) / 2
          return d
  
      if a % 2 &gt; 0:
          return lst[len(lst)/2]
  
  myList = [1,8,10,11,5]
  myList2 = [10,5,7,2,1,8]
  
  print(median(myList))
  print(median(myList2))
  </code></pre>
  
  <p>I tried doing this to fix it but still ended up with same error:</p>
  
  <pre><code>def median(list):
  
      list.sort()
  
      a = float(len(list))
  
      if a % 2 == 0:
          b = float(list[len(list)/2])
          c = float(list[(len(list)/2)-1])
          d = (b + c) / 2.0
          return float(d)
  
      if a % 2 &gt; 0:
          return float(list[len(list)/2])
  
  myList = [1,8,10,11,5]
  myList2 = [10,5,7,2,1,8]
  
  print(median(myList))
  print(median(myList2))
  </code></pre>