Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compare two lists in python, and return that the second need to have the same values regardless of order?

a = [1, 2, 3, 4]
b = [2, 4, 3, 1]
c = [2, 3]

When comparing a to b, should return True: all items in a are presented in b, and all items in b are presented in a.

When comparing a to c, should return False: there are items in a that don't exist on c.

What is the pythonic way to do it?

like image 388
Somebody still uses you MS-DOS Avatar asked Sep 24 '11 14:09

Somebody still uses you MS-DOS


1 Answers

Sort, then compare.

sorted(a) == sorted(b)
like image 51
Ignacio Vazquez-Abrams Avatar answered Oct 26 '22 02:10

Ignacio Vazquez-Abrams