How to claim two lists contain the same elements in Python?

When writing test cases, I often have to argue that two lists contain the same elements without regard to their order.

I do this by converting lists to sets.

Is there an easier way to do this?

EDIT

As @MarkDickinson noted, I can just use TestCase.assertItemsEqual .

Note that TestCase.assertItemsEqual is new in Python2.7. If you are using an older version of Python, you can use unittest2 - a reserve of new Python 2.7 features.

+127
python unit-testing
Oct 10
source share
5 answers

A slightly faster version of the implementation (if you know that most lists of pairs will have different lengths):

 def checkEqual(L1, L2): return len(L1) == len(L2) and sorted(L1) == sorted(L2) 

Comparison:

 >>> timeit(lambda: sorting([1,2,3], [3,2,1])) 2.42745304107666 >>> timeit(lambda: lensorting([1,2,3], [3,2,1])) 2.5644469261169434 # speed down not much (for large lists the difference tends to 0) >>> timeit(lambda: sorting([1,2,3], [3,2,1,0])) 2.4570400714874268 >>> timeit(lambda: lensorting([1,2,3], [3,2,1,0])) 0.9596951007843018 # speed up 
+46
Oct 10 '12 at 7:13
source share

Starting in Python 3.2, unittest.TestCase.assertItemsEqual ( doc ) has been replaced by unittest.TestCase.assertCountEqual ( doc ), which does exactly what you are looking for, as you can read from the python standard library documentation . The method is somewhat incorrectly named, but it does exactly what you are looking for.

a and b have the same elements in the same quantity, regardless of their order

Here is a simple example that compares two lists having the same elements, but in a different order.

  • using assertCountEqual test will succeed
  • when using assertListEqual test is not assertListEqual due to the difference in the order of the two lists

Here is a small script example.

 import unittest class TestListElements(unittest.TestCase): def setUp(self): self.expected = ['foo', 'bar', 'baz'] self.result = ['baz', 'foo', 'bar'] def test_count_eq(self): """Will succeed""" self.assertCountEqual(self.result, self.expected) def test_list_eq(self): """Will fail""" self.assertListEqual(self.result, self.expected) if __name__ == "__main__": unittest.main() 

Note: please make sure that the items in the lists you are comparing are sortable.

+102
Aug 05 '15 at 12:24
source share

Given

 l1 = [a,b] l2 = [b,a] 

In Python> = 3.0

 assertCountEqual(l1, l2) # True 

In Python> = 2.7, the above function was named like this:

 assertItemsEqual(l1, l2) # True 

In Python <2.7

 import unittest2 assertItemsEqual(l1, l2) # True 

Through six modules (any version of Python)

 import unittest import six class MyTest(unittest.TestCase): def test(self): six.assertCountEqual(self, self.l1, self.l2) # True 
+24
Jan 29 '16 at 23:20
source share

Converting lists to sets will tell you that they contain the same elements. But this method cannot confirm that they contain the same number of all elements. For example, your method will not work in this case:

 L1 = [1,2,2,3] L2 = [1,2,3,3] 

Most likely, you should sort the two lists and compare them:

 def checkEqual(L1, L2): if sorted(L1) == sorted(L2): print "the two lists are the same" return True else: print "the two lists are not the same" return False 

Please note that this does not alter the structure / content of the two lists. Rather, sorting creates two new lists.

+19
Oct 10 '12 at 6:59
source share

It is required to provide a library, but you can compare the list by:

provide ([1, 2]). contains_only ([2, 1])

This will not raise an exception. Thin documentation is very thin, so I would recommend looking to provide codes on github

+1
Mar 25 '15 at 15:12
source share



All Articles