Searching a sorted list#

This is like the bisect library module, but also returns whether or not the element is in the list, which saves having to do an extra comparison. Also, the function names make more sense., x)#

Return (True,i) where i is such that v[i] == x if there is such an i, or (False,j) otherwise, where j is the position where x should be inserted so that v remains sorted.


  • v – a list, which is assumed sorted

  • x – Python object


bool, int

This is implemented using the built-in bisect module.


sage: from import search
sage: search([1,4,6,7,8], 6)
(True, 2)
sage: search([1,4,6,7,8], 5)
(False, 2)
sage: search(['a','c','d','h','z'], 'e')
(False, 3)