Skip to main content

Binary search - bisect_left and bisect_right with more flexible comparison

Project description


Build Status PyPI shield

Binary search in python with more flexible comparisons.


pip install bsearch


bisect_left and bisect_right are backwards compatible with the bisect library. For example:

from bsearch import bisect_left
a = [1,2,3,4,5]
i = bisect_left(a, 4) # returns 3

You can use the key option to search arrays of different types

from bsearch import bisect_left
a = [(1,100), (2, 100), (5,200)]

# using the first element of the tuple as the key
i = bisect_left(a, 2, key=lambda x: x[0]) # returns 1

To search descending list, use the wrapper binary_search, and provide an operator. This will return the smallest i such that op(a[i], v)

from bsearch import binary_search
a = [5, 4, 3, 2]
i = binary_search(a, 4, op=lambda a b: a >= b) # returns 1

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for bsearch, version 0.0.4
Filename, size File type Python version Upload date Hashes
Filename, size bsearch-0.0.4-py3-none-any.whl (2.6 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size bsearch-0.0.4.tar.gz (1.8 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page