Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if two lists of lists are equal

Tags:

python

list

Say I have two lists of lists in Python,

l1 = [['a',1], ['b',2], ['c',3]]
l2 = [['b',2], ['c',3], ['a',1]]

What is the most elegant way to test they are equal in the sense that the elements of l1 are simply some permutation of the elements in l2?

Note to do this for ordinary lists see here, however this uses set which does not work for lists of lists.

like image 302
rwolst Avatar asked Dec 09 '22 05:12

rwolst


1 Answers

l1 = [['a',1], ['b',2], ['c',3]]
l2 = [['b',2], ['c',3], ['a',1]]
print sorted(l1) == sorted(l2)

Result:

True
like image 140
Kevin Avatar answered Dec 21 '22 04:12

Kevin