Check if one list is subset of other in Python

Python provides various methods to check if one list is a subset of another. A subset means all elements of the smaller list exist in the larger list. We'll explore three effective approaches: all() function, issubset() method, and intersection() method.

Using all() Function

The all() function returns True if all elements in an iterable are true, otherwise False. We can combine it with a generator expression to check if every element of the sublist exists in the main list −

# Define the main list and the sublist
main_list = ['Mon', 'Tue', 5, 'Sat', 9]
sub_list = ['Tue', 5, 9]

print("Main list:", main_list)
print("Sub list:", sub_list)

# Check if sub_list is a subset of main_list using all()
if all(element in main_list for element in sub_list):
    print("Sub list is a subset of the main list")
else:
    print("Sub list is not a subset of the main list")

# Test with another sublist that contains an element not in main list
sub_list2 = ['Wed', 5, 9]
print("\nTesting with:", sub_list2)
if all(element in main_list for element in sub_list2):
    print("Sub list is a subset of the main list")
else:
    print("Sub list is not a subset of the main list")
Main list: ['Mon', 'Tue', 5, 'Sat', 9]
Sub list: ['Tue', 5, 9]
Sub list is a subset of the main list

Testing with: ['Wed', 5, 9]
Sub list is not a subset of the main list

Using issubset() Method

The issubset() method is built into Python's set data structure. It checks whether all elements of one set are present in another set. We convert both lists to sets first −

main_list = [1, 2, 3, 4, 4]
sub_list = [4, 4, 2]

print("Main list:", main_list)
print("Sub list:", sub_list)

# Convert to sets and use issubset()
result = set(sub_list).issubset(set(main_list))
print("Is subset?", result)

# Test with a list that's not a subset
sub_list2 = [1, 5]
print("\nTesting with:", sub_list2)
result2 = set(sub_list2).issubset(set(main_list))
print("Is subset?", result2)
Main list: [1, 2, 3, 4, 4]
Sub list: [4, 4, 2]
Is subset? True

Testing with: [1, 5]
Is subset? False

Using intersection() Method

The intersection() method returns a set containing elements common to both sets. If the intersection equals the sublist (converted to set), then the sublist is a subset −

# Define the main list and the sublist
main_list = ['Mon', 'Tue', 5, 'Sat', 9]
sub_list = ['Tue', 5, 9]

print("Main list:", main_list)
print("Sub list:", sub_list)

# Convert to sets and use intersection()
if set(main_list).intersection(sub_list) == set(sub_list):
    print("Sub list is a subset of the main list")
else:
    print("Sub list is not a subset of the main list")

# Test with another sublist
sub_list2 = ['Wed', 5, 9]
print("\nTesting with:", sub_list2)
if set(main_list).intersection(sub_list2) == set(sub_list2):
    print("Sub list is a subset of the main list")
else:
    print("Sub list is not a subset of the main list")
Main list: ['Mon', 'Tue', 5, 'Sat', 9]
Sub list: ['Tue', 5, 9]
Sub list is a subset of the main list

Testing with: ['Wed', 5, 9]
Sub list is not a subset of the main list

Comparison

Method Performance Handles Duplicates Best For
all() O(n×m) Yes Simple cases, preserves order
issubset() O(n+m) No (sets) Large lists, best performance
intersection() O(n+m) No (sets) When you need intersection result

Conclusion

Use issubset() for the best performance with large lists. Use all() when you need to preserve duplicates or want simple readable code. Use intersection() when you also need the common elements.

Updated on: 2026-03-25T08:35:41+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements