Files
30-seconds-of-code/have_same_contents.md
Isabelle Viktoria Maciohsek 223785a49f Apply suggestions from code review
Co-Authored-By: Angelos Chalaris <chalarangelo@gmail.com>
2020-03-14 11:40:50 +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