Python is a widely used software that has many different purposes and multiple functions to perform different tasks. One useful feature of python is list functionality which helps in collecting and storing different data but many times users face problem while deleting a sublist that already exists within another sublist. So, in this article, we will learn how to delete different sublists that already exist in other sublists.
To understand the problem clearly, let's take an example where we have to delete a sublist whose data already exists in another sublist.
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
The sublists named [Shyam,John] and [David,Stefan] already have the same data in other sublists, so these extra sublists will be deleted. The output should look like this:
new_list = [[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]
Now we will understand the different ways to delete a sublist that already exists in a sublist.
Here we have mentioned different possible methods:
The easiest way to delete all sublists present in other sublists is with the help of list comprehension. Check all sublists that exist in the list and copy those that do not exist in any other sublist to the new list. Let’s take an example to understand more clearly:
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
After the code is completed, we will print the output of the above code. The output of the above code is as follows:
[[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]
All extra sublists are removed, so we wrote the correct code to remove sublists that are already in the sublist.
Another way to solve the problem is to create a completely new separate function to filter out all sublists that exist within other sublists. This can be done by defining a condition for the function and making it run accordingly.
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)]
The output of the above code is as follows:
[[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]
All extra sublists are removed, so we wrote the correct code to remove all extra sublists.
This is a very complex method for removing a sublist that already exists within another sublist. In this method, all sublists are compared with each other and the non-duplicate sublists are copied to a new list. We can understand this with the help of the following 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)
The output of the above code is as follows:
[[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]
This method is more suitable when the list is too long and contains a large number of sublists with many elements.
In this operation, sublists existing in other sublists are deleted with the help of set operation. In this approach we can convert each sublist in the list into a set and with the help of different operations we can remove all sublists present in other sublists. We can understand it more clearly through the following 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)
The output of the above code is as follows:
[[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]
All sublists present in other sublists have been deleted.
The problem of deleting a sublist that already exists in another sublist is a problem that users often face, and many times it will consume a lot of users' time. Therefore, it is possible to quickly delete all sublists that exist within another sublist using different methods suggested in the previous article.
The above is the detailed content of Python - Remove sublist that exists within another sublist. For more information, please follow other related articles on the PHP Chinese website!