Check If One List is Subset of Another in Python



Python provides various methods to check if one list is a subset of another like 'all()' function and also by using 'issubset()' function to perform this check effectively. The three primary approaches used for checking if one list is a subset of another in Python are as follows.

  • all() function : Checks if all elements in list of an iterable are true.

  • issubet() method : Used in python sets for collecting of unique elements.

  • intersection() method : To find common elements between two sets.

Using 'all()' Function

In Python all() returns 'True' if all elements in an iterable are true, otherwise, it returns False, we can this method to check if every element of a smaller list exists ina larger list by combining it with a generator expression.

Example

In the below example code 'element in Alist for element in Asub_list' refers a generator expression used with 'all()' function which yields True or False for each element in 'Asub_list', based on the result from 'all()' function we print whether 'Asub_list' is a subset of 'Alist'.

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

print("Given list ",Alist)
print("Given sublist",Asub_list)

#  Check if Asub_list is a subset of Alist using all()
if all(element in Alist for element in Asub_list):
    print("Sublist is part of the bigger list")
else:
    print("Sublist is not part of the bigger list")

#  Test with another sublist
Asub_list = ['Wed', 5, 9]
if all(element in Alist for element in Asub_list):
    print("Sublist is part of the bigger list")
else:
    print("Sublist is not part of the bigger list")

Output

Given list  ['Mon', 'Tue', 5, 'Sat', 9]
Given sublist ['Tue', 5, 9]
Sublist is part of the bigger list
Sublist is not part of the bigger list

Using 'issubet()' method

The 'issubet()' method is a built-in function provided by Python's set data structure, which checks whether all elements of one set are present in another set. we have to convert lists to sets to implement this approach and check for subsets more efficiently.

Example

In the below code, convert both 'Asub_list' and 'Alist' to sets using the ' set ()' constructor. The method set(Asub_list).issubset(set(Alist)) checks every elements in a set 'Asub_list' exists in 'Alist' set. Returns 'True' if all elements are present, otherwise prints 'False'.

Alist = [1, 2, 3, 4, 4]
Asub_list = [4, 4]

# Using issubset()
print(set(Asub_list).issubset(set(Alist)))

Output

True

Using 'intersection()' Method

This 'intersection()' method returns a set containing elements that are common in both sets. The sublist is converted to a set by comparing the intersection result and determining if the sublist is a subset of the main list.

Example

In the below example code 'Alist' is converted to a set using 'set(Alist)' and 'set(Alist).intersection(Asub_list)' will return a set of common elements in both 'Alist' and 'Asub_list'.

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


# Convert lists to sets and use intersection()
if set(Alist).intersection(Asub_list) == set(Asub_list):
    print("Sublist is part of the bigger list")
else:
    print("Sublist is not part of the bigger list")

# Test with another sublist
Asub_list = ['Wed', 5, 9]
if set(Alist).intersection(Asub_list) == set(Asub_list):
    print("Sublist is part of the bigger list")
else:
    print("Sublist is not part of the bigger list")

Output

Sublist is part of the bigger list
Sublist is not part of the bigger list
Updated on: 2024-10-14T14:11:38+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements