Comparing lists

Christian Stapfer nil at dev.nul
Mon Oct 10 08:34:35 EDT 2005


<ajikoe at gmail.com> wrote in message 
news:1128930699.799657.146210 at g47g2000cwa.googlegroups.com...
> try to use set.
>    L1 = [1,1,2,3,4]
>    L2 = [1,3, 99]
>    A = set(L1)
>    B = set(L2)
>
>    X = A-B
>    print X
>
>    Y = B-A
>    print Y
>
>    Z = A | B
>    print Z

But how "efficient" is this? Could you be a bit
more explicit on that point? What is the order
of complexity of set([...]) or of A-B, B-A,
A | B, A ^ B and A & B? - The Python Documentation
leaves me completely in the dark in this regard.

    Sorting the two lists and then extracting
A-B, B-A, A|B, A & B and A ^ B in one single
pass seems to me very likely to be much faster
for large lists.

Regards,
Christian





More information about the Python-list mailing list