Check if one list is subset of other in PythonPythonServer 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 allWe 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. ExampleLive 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")OutputRunning 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 listWith subsetIn 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. ExampleLive 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")OutputRunning 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 listUsing intersectionThe 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. ExampleLive 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")OutputRunning 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 listPradeep Elance Published on 13-May-2020 14:40:22 Previous Page Print Page Next Page Advertisements |