The best way to check if two lists have identical values
mrkafk at gmail.com
Thu Feb 25 14:30:12 CET 2010
I have stumbled upon this seemingly trivial problem: the answer is not
there in http://www.python.org/doc/faq/programming/, and googling does
not return many references really (at least for me).
I have come up with this:
def qips_identical(q, oldq):
qips = map(operator.itemgetter(1), q)
oldqips = map(operator.itemgetter(1), oldq)
if len(qips) != len(oldqips):
dif = set(qips) ^ set(oldqips)
There's a number of complications here, depending on definition of
'lists with identical values', like whether the same value can be
repeated different number of times in two lists, or whether the order of
In above example I assumed that the same values have to be repeated the
same numbers of times in both lists and that order doesn't matter so
long as the values are the same.
I was wondering if there's a better / shorter / faster way to do this --
not necessarily in the same variant, e.g. variant where order of two
lists matters would be interesting as well.
More information about the Python-list