Skip to content
Related Articles

Related Articles

Improve Article

Python | Check if a nested list is a subset of another nested list

  • Last Updated : 30 Sep, 2019

Given two lists list1 and list2, check if list2 is a subset of list1 and return True or False accordingly.

Examples:

Input : list1 = [[2, 3, 1], [4, 5], [6, 8]]
        list2 = [[4, 5], [6, 8]]
Output : True

Input : list1 = [['a', 'b'], ['e'], ['c', 'd']]
        list2 = [['g']]
Output : False

 
Let’s discuss few approaches to solve the problem.

Approach #1 : Naive Approach
Take a variable ‘exist’ which keeps track of each element, whether it is present in list1 or not. Start a loop and in each iteration ‘i’, check if ith element is present in list1. If present, set exist to True else false.




# Python3 Program to Check is a nested 
# list is a subset of another nested list
  
def checkSubset(list1, list2):
    l1, l2 = list1[0], list2[0]
    exist = True
    for i in list2:
        if i not in list1:
            exist = False
    return exist
      
# Driver Code
list1 = [[2, 3, 1], [4, 5], [6, 8]]
list2 = [[4, 5], [6, 8]]
print(checkSubset(list1, list2))
Output:



True

 
Approach #2 : Using Python set
Convert each sublist of both the given nested lists to tuples, because sets can’t hold lists as they rely on their elements being immutable and lists are mutable. But converting them to tuple works well. After this, simply check if set of list2 is a subset of list1 or not.




# Python3 Program to Check is a nested 
# list is a subset of another nested list
  
def checkSubset(list1, list2):
    temp1 = []
    temp2 = []
    for i in list1:
        temp1.append(tuple(i))
    for i in list2:
        temp2.append(tuple(i))
      
    return set(temp2) < set(temp1)
      
# Driver Code
list1 = [[2, 3, 1], [4, 5], [6, 8]]
list2 = [[4, 5], [6, 8]]
print(checkSubset(list1, list2))
Output:
True

 
Approach #3 : Using all and for loop
This method uses a for loop to check if all elements(using all) belongs to list1 or not.




# Python3 Program to Check is a nested 
# list is a subset of another nested list
  
def checkSubset(list1, list2):
    return all(x in list1 for x in list2)
      
# Driver Code
list1 = [[2, 3, 1], [4, 5], [6, 8]]
list2 = [[4, 5], [6, 8]]
print(checkSubset(list1, list2))
Output:
True

 
Approach #4 : Using map() and __contains__
In this approach we use Python map() using the “containment check” operator __contains__, checking whether list1 elements are contained withing list2 or not.




# Python3 Program to Check is a nested 
# list is a subset of another nested list
  
def checkSubset(list1, list2):
    return all(map(list1.__contains__, list2))
      
# Driver Code
list1 = [[2, 3, 1], [4, 5], [6, 8]]
list2 = [[4, 5], [6, 8]]
print(checkSubset(list1, list2))
Output:
True

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :