588 B
588 B
title, tags
| title | tags |
|---|---|
| have_same_contents | list,intermediate |
Checks if two lists contain the same elements regardless of order.
- Use
set()on the combination of both lists to find the unique values. - Iterate over them with a
forloop comparing thecount()of each unique value in each list. - Return
Falseif the counts do not match for any element,Trueotherwise.
def have_same_contents(a, b):
for v in set(a + b):
if a.count(v) != b.count(v):
return False
return True
have_same_contents([1, 2, 4], [2, 4, 1]) # True