Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Remove sublists that are present in another sublist

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 07 Jan, 2023
Improve Article
Save Article

Given a list of lists, write a Python program to remove sublists from the given list of lists that are present in another sublist. 

Examples:

Input : [['a', 'b', 'c'], ['a', 'c'], ['a', 'b', 'c'], ['d']]
Output : [['a', 'b', 'c'], ['d']]

Input : [[1], [1, 2], [1, 2, 3], [0], [0, 1]]
Output : [[1, 2, 3], [0, 1]]

  Approach #1 : Using Python Set (If order of list doesn’t matter) This approach makes use of Python sets. Create two empty lists ‘curr_res’ to store current sublist and ‘result’ to store the finalized sublists. Convert the sub-lists in the given list of lists to sets and sort them by length in reverse order, so that you can iterate through them and add each set to the curr_res only if it is not a subset of any of the existing sets in the curr_res. The only drawback of this approach is that it may produce the result in an unordered way(Since sets are unordered). 

Python3




# Python3 program to remove sublists from
# list of lists that are in another sublist
 
def removeSublist(lst):
    curr_res = []
    result = []
    for ele in sorted(map(set, lst), key = len, reverse = True):
        if not any(ele <= req for req in curr_res):
            curr_res.append(ele)
            result.append(list(ele))
         
    return result
     
# Driver code
lst = [['a', 'b', 'c'], ['a', 'b'], ['a', 'b', 'c'], ['d']]
print(removeSublist(lst))

Output:

[['c', 'b', 'a'], ['d']]

  Approach #2 : Using Python Dictionary (If order of list matters) Dict may not always produce ordered output, therefore you can use OrderedDict from collections module. 

Python3




# Python3 program to remove sublists from
# list of lists that are in another sublist
from collections import OrderedDict
 
def removeSublist(lst):
    curr_result = []
    result = []
    for ele in sorted(map(OrderedDict.fromkeys, lst), key = len, reverse = True):
        if not any(ele.keys() <= req.keys() for req in curr_result):
            curr_result.append(ele)
            result.append(list(ele))
             
    return result
     
# Driver code
lst = [['a', 'b', 'c'], ['a', 'b'], ['a', 'b', 'c'], ['d']]
print(removeSublist(lst))

Output:

[['a', 'b', 'c'], ['d']]

Approach #3 : using issubset()+list comprehension+enumerate()

Alternatively, you can use a list comprehension to create a new list that contains only the unique sublists, like this:

Python3




def remove_duplicate_sublists(lists):
    # Use list comprehension to create a new list with unique sublists
    unique_lists = [sublist for i, sublist in enumerate(lists) if sublist not in lists[:i]]
    result = [sublist for sublist in unique_lists  if
              not any(set(sublist).issubset(other) for other in unique_lists  if other != sublist)]
    return result
 
 
lists = [['a', 'b', 'c'], ['a', 'c'], ['a', 'b', 'c'], ['d'], ['d']]
print(remove_duplicate_sublists(lists))  # Output: [['a', 'b', 'c'], ['d']]
 
lists = [[1], [1, 2], [1, 2, 3], [0], [0, 1], [1, 2]]
print(remove_duplicate_sublists(lists))  # Output: [[1, 2, 3], [0, 1]]
#This code is contributed by Edula Vinay Kumar Reddy

Output

[['a', 'b', 'c'], ['d']]
[[1, 2, 3], [0, 1]]

This code defines a function remove_duplicate_sublists() that removes duplicate sublists and sublists that are present in another sublist from a list of lists. It first uses a list comprehension to create a new list with unique sublists by iterating over the sublists in the original list and checking if each sublist is not in the preceding sublists. It then uses a second list comprehension to filter out the sublists that are present in another sublist by using the issubset() method of the set() function. The resulting list is returned as the result.

The time complexity of this code is O(n^2), because it involves nested loops over the sublists in the list of lists. The space complexity is O(n), because it involves storing a list of n sublists in memory.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!