Skip to content

Instantly share code, notes, and snippets.

@ourway
Created December 21, 2013 09:57
Show Gist options
  • Save ourway/8067495 to your computer and use it in GitHub Desktop.
Save ourway/8067495 to your computer and use it in GitHub Desktop.
Efficient ways of searching sorted lists.
def bi_contains(lst, item):
"""The most efficient way of searching sorted lists.
@ref: http://goo.gl/ByYtjq
@param lst: The input list
@param item: The item i'm looking for
@return: True|False
"""
return (item <= lst[-1]) and (lst[bisect_left(lst, item)] == item)
if __name__ == '__main__':
''' Test Lines '''
a = [(1,2), (1,3), (1,4), (2,2)]
x = (1,5)
print bi_contains(a, x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment