Grendel
Grendel

Reputation: 783

Remove redundant sublists within list in python

Hello everyone I have a list of lists values such as :

list_of_values=[['A','B'],['A','B','C'],['D','E'],['A','C'],['I','J','K','L','M'],['J','M']]

and I would like to keep within that list, only the lists where I have the highest amount of values.

For instance in sublist1 : ['A','B'] A and B are also present in the sublist2 ['A','B','C'], so I remove the sublist1. The same for sublist4. the sublist6 is also removed because J and M were present in a the longer sublist5.
at the end I should get:

list_of_no_redundant_values=[['A','B','C'],['D','E'],['I','J','K','L','M']] 

other exemple =

list_of_values=[['A','B'],['A','B','C'],['B','E'],['A','C'],['I','J','K','L','M'],['J','M']]

expected output :

[['A','B','C'],['B','E'],['I','J','K','L','M']]

Does someone have an idea ?

Upvotes: 1

Views: 68

Answers (1)

Alan Abraham
Alan Abraham

Reputation: 66

mylist=[['A','B'],['A','C'],['A','B','C'],['D','E'],['I','J','K','L','M'],['J','M']]
def remove_subsets(lists):
    outlists = lists[:]
    for s1 in lists:
        for s2 in lists:
            if set(s1).issubset(set(s2)) and (s1 is not s2):
                outlists.remove(s1)
                break
    return outlists
print(remove_subsets(mylist))

This should result in [['A', 'B', 'C'], ['D', 'E'], ['I', 'J', 'K', 'L', 'M']]

Upvotes: 3

Related Questions