List is contained in other list

Python, List · Jan 7, 2021

Checks if the elements of the first list are contained in the second one regardless of order.

  • Use count() to check if any value in a has more occurrences than it has in b.
  • Return False if any such value is found, True otherwise.
def is_contained_in(a, b):
  for v in set(a):
    if a.count(v) > b.count(v):
      return False
  return True
is_contained_in([1, 4], [2, 4, 1]) # True

More like this

  • Checks if two lists contain the same elements regardless of order.

  • Finds the index of the first element in the given list that satisfies the provided testing function.

  • Finds the value of the first element in the given list that satisfies the provided testing function.