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.

sage.misc.search.search(v, 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 that a should be inserted so that v remains sorted.

INPUT:
v – a list, which is assumed sorted x – Python object
OUTPUT:
bool, int

Previous topic

Flatten nested lists

Next topic

Iterators

This Page