title: Recursive function in Python to search items within lists
body: <p>I am trying/required to write a recursive function where a True/False value is returned if an element is in a sorted list. I am required to not used the &quot;in&quot; keyword to get the answer. My code is below, I think I am close, but I can't seem to get the function to return a proper value:</p>
  <pre><code>def listSplitter(listToProcess, numToFind):
      lenOfList = len(listToProcess)
  
      if lenOfList == 1:
          if numToFind == listToProcess[0]:
              return True
          else:
              return False
      else:
  
          if lenOfList % 2 == 0:
              list1 = listToProcess[0:int(lenOfList / 2)]
              list2 = listToProcess[int(lenOfList / 2):int(lenOfList)]
  
              if list1[-1] &gt;= numToFind and list1[0] &lt;= numToFind:
                  return (list1, numToFind)
                  #return
              elif list2[-1] &gt;= numToFind and list2[0] &lt;= numToFind:
                  return listSplitter(list2, numToFind)
                  #return
          else:
              list1 = listToProcess[0:int(lenOfList / 2)]
              list2 = listToProcess[int(lenOfList / 2):int(lenOfList)]
  
              if list1[-1] &gt;= numToFind and list1[0] &lt;= numToFind:
                  return listSplitter(list1, numToFind)
                  #return
              elif list2[-1] &lt;= numToFind and list1[0] &gt;= numToFind:
                  return listSplitter(list2, numToFind)
                  #return
  
  
  
  def ordered_contains(S, x):
      return listSplitter(S,x)
      #result = listSplitter(S, x)
      #return result
  
  
  
  A = [2, 16, 26, 32, 52, 71, 80, 88]
  
  print(&quot;A contains 32: {}&quot;.format(ordered_contains(A, 32)))
  print(&quot;A contains 7: {}&quot;.format(ordered_contains(A, 7)))
  print(&quot;A contains -10: {}&quot;.format(ordered_contains(A, -10)))
  print(&quot;\n(Did those results match the earlier example?)&quot;)
  </code></pre>
  <p>Here is the output:</p>
  <pre><code>A contains 32: ([2, 16, 26, 32], 32)
  A contains 7: ([2, 16, 26, 32], 7)
  A contains -10: None
  
  (Did those results match the earlier example?)
  </code></pre>
  <p>What am I missing?</p>
  <p>Any help would be greatly appreciated.</p>
  <p>Thanks</p>