The best way to check if two lists have identical values

mk mrkafk at
Thu Feb 25 14:30:12 CET 2010

Hello everyone,

I have stumbled upon this seemingly trivial problem: the answer is not 
there in, 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):
         return False
     dif = set(qips) ^ set(oldqips)
     if len(dif):
         return False
     return True

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 
values matters.

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 mailing list