best way to compare contents of 2 lists?

Hans DushanthaKumar hans.dushanthakumar at hcn.com.au
Fri Apr 24 03:37:09 CEST 2009


'set' comes to mind, though I'm not sure if there are performance
inplications with large amopunts of data.

>>> a=[1, 2, 3]
>>> b=[2, 3, 1]
>>> set(a) == set(b)
True



Cheers,
Hans

-----Original Message-----
From: python-list-bounces+hans.dushanthakumar=hcn.com.au at python.org
[mailto:python-list-bounces+hans.dushanthakumar=hcn.com.au at python.org]
On Behalf Of Esmail
Sent: Friday, 24 April 2009 11:31 AM
To: python-list at python.org
Subject: best way to compare contents of 2 lists?

What is the best way to compare the *contents* of two different
lists regardless of their respective order? The lists will have
the same number of items, and be of the same type.

E.g. a trivial example (my lists will be larger),

a=[1, 2, 3]

b=[2, 3, 1]

should yield true if a==b

I suppose I could sort them and then compare them. I.e.,

sorted(a)==sorted(b)


I am wondering if there is a more efficient/preferred way to do so.

Thanks,
Esmail

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



More information about the Python-list mailing list