The Python Oracle

How to check if contents of two lists is same in python?

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

--

Track title: CC O Beethoven - Piano Sonata No 3 in C

--

Chapters
00:00 Question
01:07 Accepted answer (Score 4)
01:45 Thank you

--

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

--

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

--

Tags
#python #django #list

#avk47



ACCEPTED ANSWER

Score 4


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.