title: List slicing and finding second highest value python
body: <p>I need to write a function that takes a list as a parameter and find the <strong>second highest value</strong> in the list.  Return that value.  If there is only one value in the list, return that value.  If the list is empty, return 0.</p>
  
  <p>To create the list, I prompt the user for numbers until the user enters a sentinel value of -1.  Then I prompt the user for a beginning location (loc) and a length (len). I would extract a list slice that begins at index loc and has length len and then use my function to find the second highest value in that list slice.</p>
  
  <p>Here's the code I have so far:</p>
  
  <pre><code>userList = []
  
  def main():
      inputList = int(input("Enter a number &gt;= 0. Enter -1 to end input: "))
      while inputList != -1:
          userList.append(inputList)
          inputList = eval(input("Enter a number &gt;= 0. Enter -1 to end input: "))
  
      return extractList(userList)
  
  def extractList(userList):
      loc = int(input("Enter a starting location: "))
      length = int(input("Enter a lenghth: "))
  
      selSlice = userList[loc:loc + length]
  
      if len(selSlice) == 1:
          return selSlice[0]
      if len(selSlice) == 0:
          return 0
  
      num = max(selSlice)
      occ = userList.count(num)
      for i in range(occ):
          userList[userList.index(num):userList.index(num)+1] = ''
  
      print("Second highest value in ", selSlice, "is ", max(selSlice))
  
  main()
  </code></pre>
  
  <p>I'm testing to see if the slice works, but it seems to take the starting index of loc and goes to ending index of len instead of going out the length of len.</p>
  
  <p>Example, if I have a list:</p>
  
  <pre><code>[1, 3, 7, 21, 37, 76, 23, 91, 15, 11, 2, 4]
  </code></pre>
  
  <p>and my <code>loc</code> is 3 and my <code>len</code> is 6, the result should be <code>[21, 37, 76, 23, 91, 15]</code>. However, I'm not getting this desired result, instead I would get <code>[21, 37, 76]</code>.</p>
  
  <p>What should my <code>extractList(a)</code> be? Also, if you could help me with the function to find the second highest value, it'd be great. Thanks for any input!</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Ok, so I'm on the right track now, thanks to <a href="https://stackoverflow.com/users/635148/chris-arena">Chris Arena</a> and <a href="https://stackoverflow.com/users/3113477/to-click-or-not-to-click">To Click or Not to Click</a>. (Code has been updated)</p>
  
  <p>However, the code above gives me the second highest value of the whole list, not the sliced list. I'm not sure if all the variables are correct.</p>
  
  <p>If my <code>userList</code> is <code>[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]</code> and my loc is 6 and length is 4, I get <code>[4, 6, 8, 10]</code> back, as I should, but the second highest value of the slice is 9, which is the second highest of userList, not the slice.</p>
  
  <p>I tried to change <code>userList</code> to <code>selSlice</code> starting from line <code>if len(userList) == 1:</code> through to the end to see if that made a difference. It did, but the results were questionable (aka wrong). I used the same userList, loc, and length as mentioned in the previous paragraph. I got <code>[4, 6, 8]</code> back as the slice (wrong) and the second highest value is 8, which is wrong for the slice the program returned but right for my slice that I requested. So I'm not sure what could be wrong here. Any advice?</p>
  
  <p><strong>Edit to Edit:</strong></p>
  
  <p>My latest code shows the correct slice, but the wrong 2nd highest value. I'm getting:
  <code>Second highest value in  [4, 6, 8, 10] is  10</code> Not sure what needs fixing =\</p>