title: Get a percentage range of a list in python
body: <p>I'm trying to find the data range of a given percentage in a list. I want my result to be the highest and lowest extremes of that range. This is the simplest workable solution I have so far. 'The string formatting is just for testing, when I implement this it will only return the desired values'. If you don't understand what I am trying to achieve then run my code and you should be able to understand what I am trying to accomplish.</p>
  
  <pre><code>#Sample Data List
  data = [
      4.252, 5.878, 1.435, -0.112, 6.97, -1.812, 0.887, 1.724, -1.452, 5.193,
      -1.001, 11.085, 1.335, 11.763, -7.284, -1.733, 3.175, 3.077, 0.249, -2.095,
      -0.253, 11.095, 5.184, 1.45, -3.882, 8.251, -4.234, -0.096, 5.957, -4.473,
      -3.874, 3.246, -0.929, -1.707, 2.079, 0.647, -8.189, 2.074, 3.226, 0.123,
      -3.097, -5.706, 4.276, 7.377, -5.861, 0.357, 0.966, -0.63, 2.129, 1.514,
      6.82, -1.854, 8.394, 5.141, -4.501, 1.307, -2.812, -0.348, 2.729, 2.232,
      4.096, -2.257, -4.802, -1.116, 1.346, 4.733, -1.391, -2.986, 2.8, 4.949,
      -4.101, 2.727, -1.267, -15.034, 5.322, 7.011, 6.36, 0.16, 0.719, 4.302,
      1.365, 3.098, -2.892, -4.589, -5.516, 2.182, -6.019, -6.75, 15.499, -4.58,
      3.516, 1.18, 9.648, -1.21, -10.911, -0.583, -0.545, 3.286, 0.51, 9.578,
      -2.444, 2.771, -3.846, -3.819, 1.877, 1.392, -2.784, -5.907, 4.206, 1.42,
      -2.279, -0.358, -0.649, 2.052, 1.04, 1.764, -3.156, -2.685, 0.106, 3.569,
      0.944, 3.797, -0.76, 4.89, -3.014, -2.118, -1.142, -1.578, -8.84, 3.034,
      -2.693, -2.989, -0.815, -2.504, 3.147, 1.596, -2.94, 0.906, -0.154, -3.765
       ]
  
  #Input for percent range
  percent = 50
  
  #Get half of percentage
  halfPercent = int(int((len(data) * float('.'+str(percent))/2)))
  
  #Build string for printing later and create a counter
  string = str(percent)+'% of data entries are beween values '
  counter = 0
  
  #Sort data and iterate up until it get to the counter gets to the halfPercent
  for item in sorted(data):
      if item &gt;= 0:
          counter += 1
          if counter &gt;= halfPercent:
              string += str(item)
              counter = 0
              break
  string += ' and '
  
  #Sort data and iterate up until it get to the counter gets to the halfPercent
  for item in sorted(data, reverse=True):
      if item &lt;= 0:
          counter += 1
          if counter &gt;= halfPercent:
              string += str(item)
              break
  
  #Do the printing
  print 'Min is ' + str(min(data)) + ' and Max is ' + str(max(data))
  print string
  </code></pre>
  
  <p>This seems like it is not very pythonic and its a lot of code for the little it is doing. What better and more efficient methods are there?</p>
  
  <p>Edit and Update...
  Using and changing slightly the answer code that @DMfll posted helped a lot. Here is my new code.</p>
  
  <pre><code>#Sample data
  data = [
      4.252, 5.878, 1.435, -0.112, 6.97, -1.812, 0.887, 1.724, -1.452, 5.193,
      -1.001, 11.085, 1.335, 11.763, -7.284, -1.733, 3.175, 3.077, 0.249, -2.095,
      -0.253, 11.095, 5.184, 1.45, -3.882, 8.251, -4.234, -0.096, 5.957, -4.473,
      -3.874, 3.246, -0.929, -1.707, 2.079, 0.647, -8.189, 2.074, 3.226, 0.123,
      -3.097, -5.706, 4.276, 7.377, -5.861, 0.357, 0.966, -0.63, 2.129, 1.514,
      6.82, -1.854, 8.394, 5.141, -4.501, 1.307, -2.812, -0.348, 2.729, 2.232,
      4.096, -2.257, -4.802, -1.116, 1.346, 4.733, -1.391, -2.986, 2.8, 4.949,
      -4.101, 2.727, -1.267, -15.034, 5.322, 7.011, 6.36, 0.16, 0.719, 4.302,
      1.365, 3.098, -2.892, -4.589, -5.516, 2.182, -6.019, -6.75, 15.499, -4.58,
      3.516, 1.18, 9.648, -1.21, -10.911, -0.583, -0.545, 3.286, 0.51, 9.578,
      -2.444, 2.771, -3.846, -3.819, 1.877, 1.392, -2.784, -5.907, 4.206, 1.42,
      -2.279, -0.358, -0.649, 2.052, 1.04, 1.764, -3.156, -2.685, 0.106, 3.569,
      0.944, 3.797, -0.76, 4.89, -3.014, -2.118, -1.142, -1.578, -8.84, 3.034,
      -2.693, -2.989, -0.815, -2.504, 3.147, 1.596, -2.94, 0.906, -0.154, -3.765
  ]
  
  #Percent value needs to be variable
  percent = 90
  
  #Sorting and finding the percentage of data
  half_way = int(len(data)*(float(""".{}""".format(percent))/2))
  
  #Dictionary that will be returned from function later
  returnDict = {
      'max' :  max(data),
      'min' : min(data),
      'high' : max([item for item in sorted(data) if item &gt;= 0][0:half_way]),
      'low' : min([item for item in reversed(sorted(data)) if item &lt;= 0][0:half_way])
  }
  #Print simply to view data
  print """Max is {}, Min is {} and {}% of data entries are between values {} and {}""".format(returnDict['max'],
                                                                                               returnDict['min'],
                                                                                               percent,
                                                                                               returnDict['low'],
                                                                                               returnDict['high'])
  </code></pre>
  
  <p>Output looks like this.</p>
  
  <pre><code>Max is 15.499, Min is -15.034 and 90% of data entries are between values -8.84 and 6.36
  </code></pre>
  
  <p>Am still wondering if it can be done with less code. Maybe with numpy or another library?</p>