Skip to main content

How to Check If All Elements Are in Another List in Python

Checking if one list is a subset of another (meaning every element in the first list exists within the second) is a fundamental operation in data validation, filtering, and set theory logic. While Python provides the simple in operator for single items, checking an entire collection requires slightly different approaches depending on your performance needs and data types.

This guide explores the most Pythonic method using all(), the high-performance method using set(), and how to handle unhashable data types.

Method 1: Using the all() Function (Pythonic)

The all() function is the most readable way to solve this problem. It iterates through the subset list and checks if each element exists in the superset list.

Basic Implementation

This method works well for smaller lists and preserves the logic of "checking every item."

subset = [2, 4, 6]
superset = [1, 2, 3, 4, 5, 6, 7]

# ✅ Correct: Check if every item in 'subset' exists in 'superset'
result = all(item in superset for item in subset)

print(f"Is subset contained in superset? {result}")

Output:

Is subset contained in superset? True

Understanding the Logic

If a single element in subset is missing from superset, all() stops immediately (short-circuits) and returns False.

subset = [2, 99] # 99 is missing
superset = [1, 2, 3]

# ⛔️ Failed Check
if all(item in superset for item in subset):
print("All elements found.")
else:
print("Some elements are missing.")

Output:

Some elements are missing.
note

Performance Check: Using all() with lists has a time complexity of roughly O(N*M) because for every item in the subset, Python scans the superset linearly. For large lists, Method 2 is preferred.

Method 2: Using Set Operations (High Performance)

If the order of elements doesn't matter and your data consists of hashable items (strings, numbers, tuples), converting lists to sets is significantly faster.

Using issubset()

Sets allow O(1) lookups, reducing the total complexity to O(N+M).

subset_list = [10, 20]
superset_list = [10, 20, 30, 40]

# ✅ Correct: Convert to sets and check relationship
is_contained = set(subset_list).issubset(set(superset_list))

# Alternative syntax using <= operator
is_contained_operator = set(subset_list) <= set(superset_list)

print(f"Is contained? {is_contained}")
print(f"Operator check: {is_contained_operator}")

Output:

Is contained? True
Operator check: True
tip

Using set(A) <= set(B) is the mathematical notation for "A is a subset of B". It is functionally equivalent to .issubset().

Method 3: Handling Duplicates (Strict Inclusion)

Both all() and set() ignore element frequency.

  • subset = [1, 1, 1]
  • superset = [1, 2]

Both methods above return True because the value 1 exists in the superset. If you need to ensure that the superset has enough counts of duplicate items (e.g., you need three 1s), use collections.Counter.

from collections import Counter

subset = ['a', 'a', 'b']
superset = ['a', 'b', 'c'] # Only one 'a'

# ⛔️ Standard set check returns True (incorrect for inventory logic)
print(f"Set check: {set(subset).issubset(set(superset))}")

# ✅ Correct: Counter checks counts
subset_counts = Counter(subset)
superset_counts = Counter(superset)

# Check if superset has equal or more counts for every item in subset
has_enough_items = all(superset_counts[k] >= v for k, v in subset_counts.items())

print(f"Count check: {has_enough_items}")

Output:

Set check: True
Count check: False

Common Pitfalls: Unhashable Types

Sets require elements to be "hashable" (immutable). You cannot create a set of lists or dictionaries. If your list contains mutable objects, Method 2 will crash.

Error

list_of_lists_A = [[1], [2]]
list_of_lists_B = [[1], [2], [3]]

try:
# ⛔️ Error: Lists are unhashable
print(set(list_of_lists_A).issubset(set(list_of_lists_B)))
except TypeError as e:
print(f"Error: {e}")

Output:

Error: unhashable type: 'list'

Solution

Fallback to Method 1 (all()) for unhashable types.

list_of_lists_A = [[1], [2]]
list_of_lists_B = [[1], [2], [3]]

# ✅ Correct: Use all() for unhashable types
result = all(item in list_of_lists_B for item in list_of_lists_A)

print(f"Unhashable containment check: {result}")

Output:

Unhashable containment check: True

Conclusion

To check if list A is contained in list B:

  1. Use set(A).issubset(set(B)) for best performance on large lists of numbers or strings.
  2. Use all(x in B for x in A) for smaller lists or lists containing unhashable objects (like other lists).
  3. Use collections.Counter if the frequency of duplicates matters (e.g., inventory systems).