The Python Oracle

Check if two unordered lists are equal

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC D Schuberts Piano Sonata D 850 in D

--

Chapters
00:00 Question
00:34 Accepted answer (Score 590)
01:28 Answer 2 (Score 76)
02:03 Answer 3 (Score 58)
02:27 Answer 4 (Score 20)
03:09 Thank you

--

Full question
https://stackoverflow.com/questions/9623...

Accepted answer links:
[Documentation on ]: http://docs.python.org/library/stdtypes....
[multiset]: http://en.wikipedia.org/wiki/Multiset
[collections.Counter]: http://docs.python.org/dev/library/colle...

Answer 2 links:
[timsort]: http://hg.python.org/cpython/file/2.7/Ob...
[sorted()]: http://docs.python.org/library/functions...

Answer 3 links:
[Check if two unordered lists are equal]: https://stackoverflow.com/questions/9623...
[this answer]: https://stackoverflow.com/a/9623607/4279

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #list #comparison

#avk47



ACCEPTED ANSWER

Score 618


Python has a built-in datatype for an unordered collection of (hashable) things, called a set. If you convert both lists to sets, the comparison will be unordered.

set(x) == set(y)

Documentation on set


EDIT: @mdwhatcott points out that you want to check for duplicates. set ignores these, so you need a similar data structure that also keeps track of the number of items in each list. This is called a multiset; the best approximation in the standard library is a collections.Counter:

>>> import collections
>>> compare = lambda x, y: collections.Counter(x) == collections.Counter(y)
>>> 
>>> compare([1,2,3], [1,2,3,3])
False
>>> compare([1,2,3], [1,2,3])
True
>>> compare([1,2,3,3], [1,2,2,3])
False
>>> 



ANSWER 2

Score 84


If elements are always nearly sorted as in your example then builtin .sort() (timsort) should be fast:

>>> a = [1,1,2]
>>> b = [1,2,2]
>>> a.sort()
>>> b.sort()
>>> a == b
False

If you don't want to sort inplace you could use sorted().

In practice it might always be faster then collections.Counter() (despite asymptotically O(n) time being better then O(n*log(n)) for .sort()). Measure it; If it is important.




ANSWER 3

Score 65


sorted(x) == sorted(y)

Copying from here: Check if two unordered lists are equal

I think this is the best answer for this question because

  1. It is better than using counter as pointed in this answer
  2. x.sort() sorts x, which is a side effect. sorted(x) returns a new list.



ANSWER 4

Score 21


You want to see if they contain the same elements, but don't care about the order.

You can use a set:

>>> set(['one', 'two', 'three']) == set(['two', 'one', 'three'])
True

But the set object itself will only contain one instance of each unique value, and will not preserve order.

>>> set(['one', 'one', 'one']) == set(['one'])
True

So, if tracking duplicates/length is important, you probably want to also check the length:

def are_eq(a, b):
    return set(a) == set(b) and len(a) == len(b)