Files
30-seconds-of-code/snippets/have_same_contents.md
Angelos Chalaris 26772c3ee2 Move snippets in the correct folder
Related #188
2020-03-15 12:54:08 +02:00

609 B

title, tags
title tags
have_same_contents list,intermediate

Returns True if two lists contain the same elements regardless of order, False otherwise.

Use set() on the combination of both lists to find the unique values. Iterate over them with a for loop comparing the count() of each unique value in each list. Return False if the counts do not match for any element, True otherwise.

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