Python - Remove Sublists that are Present in Another Sublist


Python is widely used software which has many different purposes of use and a huge variety of feature to perform different tasks. One such useful feature of python is the list feature which help to collect and store different data but many−a−times the user face an issue in removing the sublists which are already present in another sublist. So, in this article we are going to learn how to remove the different sublist that are already present in other sublist.

To understand the problem clearly let’s take an example where we have to remove the sublists whose data already exists in another sublist.

Example

duplicate_list = [[Aayush, Shyam, John], [Shyam, John], [Henry, Joe], [David, Stefen, Damon], [David, Stefen]]
#All the sublist whose data is already present in other sublist are to be removed

Output

The sublist having the name [Shyam,John] and [David,Stefan] already have the same data in other sublist and thus these extra sublists are to be removed. The output should look as follows:

new_list = [[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]

Now we will learn about the different ways possible to remove the sublists that are already present in sublist.

Here we have mentioned different possible methods:

List Comprehension

The easiest method to remove all the sublists present in other sublist is with the help of list comprehension. All the sublist present in the list are checked and those sublist which are not present in any other sublist are copied into the new list. Let’s take an example to understand more clearly:

Example

duplicate_list = [[Aayush, Shyam, John], [Shyam, John], [Henry, Joe], [David, Stefen, Damon], [David, Stefen]]
New_list = [sublist for sublist in duplicate_list if not any(set(sublist) <= set(other) for other in duplicate_list if sublist is not other)]
#We first check all the lists of the duplicate list through the any() function and then we check for any repeatation with the help of <= operator

Output

Once the code is done, we will print the output of the above code. The output of the above code will be as follows:

[[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]

All the extra sublist were removed and thus we wrote the correct code to remove the sublist which is already present in the sublist.

Defining Function

A different approach to solve the problem is by creating a whole new separate function to filter out all the sublist which are present in other sublists. This can be done by defining a condition for the function and make it run accordingly.

Example

def is_sublist(sublist, other):  #is_sublist is the function defined
    return set(sublist) <= set(other)  #the functions checks 2 sublists at a time and if the sublist already exists then it returns with `true` feedback and does not consider the extra sublist

duplicate_list = [[Aayush, Shyam, John], [Shyam, John], [Henry, Joe], [David, Stefen, Damon], [David, Stefen]]
new_list = [sublist for sublist in duplicate_list if not any(is_sublist(sublist, other) for other in duplicate_list if sublist is not other)]

Output

The output for the above code will be as follows:

[[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]

All the extra sublists were removed and thus we wrote the correct code to remove all the extra sublists.

Comparing Each List

This is a very complex method of removing sublists which are already present in another sublist. In this method all the sublists are compared with one another and the sublists which are not repeating are copied into a new list. We can understand the same with the help of following example:

Example

duplicate_list = [[Aayush, Shyam, John], [Shyam, John], [Henry, Joe], [David, Stefen, Damon], [David, Stefen]]
#A copy of duplicate list is created to avoid any errors in the original file
new_list = duplicate_list[:]

#Check each sublist present in the new_list
for sublist in duplicate_list:
    for other in new_list:
        # Checking of presence of sublist present in other sublist is done
        if sublist != other and set(sublist).issubset(other):   #issubset is used to check presence of one sublist in another sublist
            # Remove all the repeating sublist
            new_list.remove(sublist)
            break  #break is used to stop the loop so that it does not keep checking continuosly

print(new_list)

Output

The output of the above code will be as follows:

[[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]

This method is preferable in the case when the list is too long and contains a lot of sublist with a lot of elements.

Set Operations

In this operation the sublist which are present in other sublist are removed with the help of set operation. In this method we can convert each sublist in the list into a set and with the help of different operations we can remove all the sublists present in other sublist. We can understand it more clearly through the following example:

Example

duplicate_list = [[Aayush, Shyam, John], [Shyam, John], [Henry, Joe], [David, Stefen, Damon], [David, Stefen]]

new_list = []

for sublist in duplicate_list:
    is_subset = False
    for other in duplicate_list:
        if sublist != other and set(sublist).difference(set(other)) == set():  #The difference operation is used to calculate the difference betwen two sets
            is_subset = True  #When the sublist is present in another sublist the result of is_subset will be true 
            break  #Once the result is found to be true, the loop is broke and all the other sublist are copied into the new_list
    if not is_subset:
        new_list.append(sublist)

print(new_list)

Output

The output of the above code will be as follows:

[[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]

All the sublists present in other sublist were removed.

Conclusion

This issue of removing the sublist which are already present in another sublist is a very commonly faced issue by the user and many−a−times it can result into consuming a lot of time of the user. So, one can use the different methods suggested in the above article to remove all the sublist present inside another sublist in a speedy manner.

Updated on: 01-Aug-2023

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements