The best way to check if two lists have identical values

M

mk

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
 
P

Peter Otten

mk said:
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
 
A

Arnaud Delobelle

mk said:
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.
 
M

mk

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
 
S

Steven D'Aprano

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top