Reputation: 394
I have 3 lists :
list_1 = [1,2]
list_2 = [2,1]
list_3 = [1,2,3]
Note: numbers inside [] are the ids from Django Model
I want to test whether the contents (but not necessarily the order) of two lists are exactly the same. With reference to the 3 examples above:
Comparing list_1 and list_2
should return True,
but if I do validation between list_2 and list_3, or between list_1 and list_3,
then the result should be False.
How do I achieve this?
Thanks :D
Upvotes: 4
Views: 5923
Reputation: 1749
I interpret your question as return true if the contents (but not necessarily order) of the lists are identical, otherwise return false. This can be solved by sorting both lists, then using the == for comparison. sorted() returns a list of integers in ascending order. This means that if the lists' contents are the same, sorted() returns identical lists.
def validation(list_1,list_2):
return sorted(list_1) == sorted(list_2)
This passes all of your test cases. I might have misunderstood your question, please clarify if that's the case.
Upvotes: 4