John Salerno wrote:
> If I want to make a list of four items, e.g. L = ['C', 'A', 'D', 'B'],
> and then figure out if a certain element precedes another element, what
> would be the best way to do that?
>
> Looking at the built-in list functions, I thought I could do something like:
>
> if L.index('A') < L.index('D'):
>      # do some stuff

This actually performs pretty well since list.index is implemented in
C.

The obvious (to me) implementation of:
def before(lst, a, b):
    for x in lst:
        if x == a:
            return True
        if x == b:
            return False

runs about 10-50 times faster than the double index method if I use
psyco.  Without psyco, it ends up being faster for the cases where a or
b appears early on in the list, and the other appears towards the end.

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to