Check if one list is subset of other in Python

PythonServer Side ProgrammingProgramming

In text analytics and various other fields of data analytics it is often needed to find if a given list is already a part of a bigger list. In this article we will see the python programs to implement this requirement.

With all

We use a for loop to check if every element of the smaller list is present in the bigger list. The all function ensures each evaluation returns true.

Example

 Live Demo

Alist = ['Mon','Tue', 5, 'Sat', 9]
Asub_list = ['Tue',5,9]

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

# With all
if (all(x in Alist for x in Asub_list)):
   print("Sublist is part of bigger list")
else:
   print("Sublist is not part of bigger list")

# Checkign again
Asub_list = ['Wed',5,9]
print("New sublist",Asub_list)
if (all(x in Alist for x in Asub_list)):
   print("Sublist is part of bigger list")
else:
   print("Sublist is not part of bigger list")

Output

Running the above code gives us the following result −

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

With subset

In this approach we convert the lists into set and use the subset functions to validate if the small list is part of the bigger list or not.

Example

 Live Demo

Alist = ['Mon','Tue', 5, 'Sat', 9]
Asub_list = ['Tue',5,9]

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

# With all
if(set(Asub_list).issubset(set(Alist))):
   print("Sublist is part of bigger list")
else:
   print("Sublist is not part of bigger list")

# Checkign again
Asub_list = ['Wed',5,9]
print("New sublist",Asub_list)
if(set(Asub_list).issubset(set(Alist))):
   print("Sublist is part of bigger list")
else:
   print("Sublist is not part of bigger list")

Output

Running the above code gives us the following result −

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

Using intersection

The intersection function find the common elements between two sets. In this approach we convert the lists into sets and apply the intersection function. If the result of intersection is same as the sublist then we conclude the sublist is part of thelist.

Example

 Live Demo

Alist = ['Mon','Tue', 5, 'Sat', 9]
Asub_list = ['Tue',5,9]

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

# With all
if(set(Alist).intersection(Asub_list)== set(Asub_list)):
   print("Sublist is part of bigger list")
else:
   print("Sublist is not part of bigger list")

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

Output

Running the above code gives us the following result −

Given list ['Mon', 'Tue', 5, 'Sat', 9]
Given sublist ['Tue', 5, 9]
Sublist is part of bigger list
New sublist ['Wed', 5, 9]
Sublist is not part of bigger list
raja
Published on 13-May-2020 18:10:22
Advertisements