title: What is the most efficient way of checking if value exists in a range of start and end positions in Python?
body: <p>Lets say I have a text file that has start and end positions like this:</p>
  
  <pre><code>Start End
    1     5
   11    14
   15    19
   23    30
  </code></pre>
  
  <p>I want to check if a given set of values exists in between and including those positions, e.g. 4,14,20  would return TRUE,TRUE,FALSE. </p>
  
  <p>What would be the most efficient way of doing so?</p>
  
  <p>Idea 1) I could generate each possible number into a list and check if value is in list - pseudo code would look like this:</p>
  
  <pre><code>list = []
  values = [4,14,20]
  for line in file:
      for position in range(int(line.split()[0]),int(line.split()[1])+1):
          list.append(position) #Populate list with every viable position
  
  for value in values:
      if value in list:
          print("TRUE")
      else:
          print("FALSE")
  </code></pre>
  
  <p>Idea 2) Instead of saving each possible position into the list, save only the start and end but then iterate through each range when checking:</p>
  
  <pre><code>list = []
  for line in file:
      list.append(line) #Save only start and end into list
  
  for value in values:
      for start_end in list:
          for position in range(int(start_end.split()[0]),int(start_end.split()[1])+1):
              if value == position:
                  print("TRUE")
  </code></pre>
  
  <p>If my file is very large I suspect idea 1 would take up a lot of memory, but on the other hand would idea 2 take much longer to run as it has to iterate through so much more?</p>
  
  <p>Or is there some completely different way that is better?
  Many thanks!</p>