[Tutor] Searching Sorted Lists

Kent Johnson kent37 at tds.net
Fri Aug 19 21:40:43 CEST 2005

DaSmith at technip.com wrote:
> Hi, Can someone tell me if there is a bulit in Binary search function for
> python lists ?

The bisect module implements a binary search though it doesn't support custom comparison functions. You could make your list elements be instances of a class with a __cmp__() method that does what you want. You should also consider whether you can use a dict to hold your data - if you need fast lookup and order is not important that is the way to go.


> I am currently building lists and sorting them with a comparison function.
> The only list search function I know is List.Index(X), which is pretty
> inefficient I reckon, especially hen the lists are likely to contain 100's
> or 100's of members.
> Is there a search function that uses a compariosn function / binary chop ?
> or will I have to implement my own ?
> Regards,
> Daniel Smith.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list