Velocity Reviews > The best way to check if two lists have identical values

# The best way to check if two lists have identical values

mk
Guest
Posts: n/a

 02-25-2010
Hello everyone,

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):
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.

Regards,
mk

Peter Otten
Guest
Posts: n/a

 02-25-2010
mk wrote:

> 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)

I don't think the above is a relevant part of the problem.

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

No you haven't. [1, 1, 0] and [1, 0, 0] are considered equal by your
algorithm.

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

sorted(left_list) == sorted(right_list)

should be good enough in most cases where you do care for repetitions but
not order.

Peter

Arnaud Delobelle
Guest
Posts: n/a

 02-25-2010

mk wrote:
> Hello everyone,
>
> 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):
> 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.

Your code checks if the two lists have the same length and the same
elements, but not necessarily the same number of each elements. E.g.

qips = [1, 1, 2]
oldqips = [1, 2, 2]

will return True

If you want to check if each value has the same number of occurences,
you can do

return sorted(qips) == sorted(oldqips)

assuming that all elements in the lists are comparable.

--
Arnaud

mk
Guest
Posts: n/a

 02-25-2010
On 2010-02-25 14:55, Arnaud Delobelle wrote:

> Your code checks if the two lists have the same length and the same
> elements, but not necessarily the same number of each elements. E.g.
>
> qips = [1, 1, 2]
> oldqips = [1, 2, 2]
>
> will return True
>
> If you want to check if each value has the same number of occurences,
> you can do
>
> return sorted(qips) == sorted(oldqips)
>
> assuming that all elements in the lists are comparable.

Thanks!

Regards,
mk

Steven D'Aprano
Guest
Posts: n/a

 02-25-2010
On Thu, 25 Feb 2010 14:30:12 +0100, mk wrote:

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

sorted(list1) == sorted(list2)

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

list1 == list2

--
Steven