0

If you have a list such as this:

foo_list = [x for x in xrange(2, 2000000000, 2)] ## [2, 4, 6, 8, ...]

And you want to check if an element is inside it, then the following operation needs to do 1 billion comparisons:

x = 3
if x in foo_list:
    pass

However, since we know that foo_list is sorted, we can evaluate the conditional after two comparisons (at most).

Is there a Pythonic way to take advantage of this behavior? I'm wondering if there's a built-in way to tell the program that we're looking for an element in a sorted list:

x = 3
if is_in(x, foo_list, sorted=True)
    pass
martineau
  • 112,593
  • 23
  • 157
  • 280
touch my body
  • 1,432
  • 19
  • 31

0 Answers0