5 Best Ways to Check if a List is Contained in Another List in Python

πŸ’‘ Problem Formulation: In Python, a common circumstance arises when we need to check if all elements of one list are contained within another list. This could be part of a data validation step, searching for subsets, or simply comparing datasets. For example, given list1 = [1, 2, 3] and list2 = [1, 2, 3, 4, 5], we want to verify that list1 is indeed a subset of list2.

Method 1: Using a For-Loop

This method involves iterating through each element of the smaller list and checking if it is present in the larger list. This approach is straightforward and does not require any built-in Python functions beyond the ‘in’ operator which checks for the presence of an element in a list.

Here’s an example:

list1 = [1, 2, 3]
list2 = [1, 2, 3, 4, 5]
is_subset = True
for item in list1:
    if item not in list2:
        is_subset = False
        break
print(is_subset)

Output: True

This code snippet defines two lists and then uses a for-loop to iterate through the first list, checking each item’s presence in the second list. If any item is not found, it sets the flag is_subset to False and breaks the loop to avoid unnecessary checks.

Method 2: Using the all() Function

The all() function in Python returns True if all elements within an iterable are true. Combined with a generator expression, it provides a clean, readable one-liner to check if a list is contained in another list.

Here’s an example:

list1 = [1, 2, 3]
list2 = [1, 2, 3, 4, 5]
is_subset = all(item in list2 for item in list1)
print(is_subset)

Output: True

This code utilizes the built-in all() function with a generator expression to check for the presence of each element from list1 in list2. It’s a concise way to get the same result as the for-loop, but with less code.

Method 3: Using Set Conversion

When order and duplicate elements are not of concern, converting both lists to sets and using set comparison can determine if one list is a subset of another. Sets in Python are collections of unordered, unique items that provide efficient membership tests.

Here’s an example:

list1 = [1, 2, 3]
list2 = [1, 2, 3, 4, 5]
is_subset = set(list1).issubset(set(list2))
print(is_subset)

Output: True

This code converts both lists to sets and then uses the issubset() method of a set to check if all elements of the first set (derived from list1) are in the second set (derived from list2). It’s an efficient method for larger datasets.

Method 4: Using the <= Operator on Sets

Similarly to Method 3, we can leverage the properties of sets to determine list containment. The <= operator between sets is the mathematical equivalent of the subset test and returns True if the set on the left is a subset of the set on the right.

Here’s an example:

list1 = [1, 2, 3]
list2 = [1, 2, 3, 4, 5]
is_subset = set(list1) <= set(list2)
print(is_subset)

Output: True

This code example shows the use of the subset operator <= between two sets created from our lists. It achieves the same result as using the issubset() method but in an even more concise syntax.

Bonus One-Liner Method 5: Using List Comprehension

This concise method applies list comprehension to filter elements that are not in the second list. If the filtered list is empty, then the first list is a subset of the second.

Here’s an example:

list1 = [1, 2, 3]
list2 = [1, 2, 3, 4, 5]
is_subset = not [item for item in list1 if item not in list2]
print(is_subset)

Output: True

This one-liner uses list comprehension to create a list of items that are in list1 but not in list2. By negating the result, we convert that list to a boolean value which becomes True if the list is empty (meaning list1 is a subset of list2).

Summary/Discussion

  • Method 1: For-Loop. Simple and straightforward. Inefficient for large lists due to its O(n*m) complexity.
  • Method 2: all() Function. More Pythonic and one-liner. Still retains O(n*m) complexity but offers better readability.
  • Method 3: Set Conversion & issubset(). Efficient for large datasets. Loses order and cannot handle duplicate elements.
  • Method 4: Set <= Operator. As efficient as Method 3. Has the same pros and cons as set conversion but uses operator syntax for clarity.
  • Bonus Method 5: List Comprehension. Compact and clever. Suffers performance-wise with large lists and is less readable to some.