Home > Software design >  How to remove duplicate value from nested list?
How to remove duplicate value from nested list?

Time:07-07

Here is my list:

lists=[[2], [3], [4, 5, 6], [7], [8], [9], [9, 10, 11], [10]]

I would like to remove values that duplicated with other elements.

Among the list, [9] and [10] are in [9, 10, 11]

I want [9, 10, 11] to be remained, but remove single [9] and [10]

Could anyone tell me how could I do that?

I hope the list could be:

lists=[[2], [3], [4, 5, 6], [7], [8], [9, 10, 11]]

CodePudding user response:

Here's my approach.

  1. We'll use a set (call this a) to store all the elements that appear in lists length longer than 1.
  2. We'll go through the single element lists. If it appears in the set, we shouldn't add it. Otherwise, add it.

Below is an implementation (which should hopefully be easy to understand and read, as well as being thoroughly commented):

lists = [[2], [3], [4, 5, 6], [7], [8], [9], [9, 10, 11], [10]]
ans = []
a = set() 
for i in lists:
    if len(i) > 1: #if this is a list with more than 1 element
        for j in i: #go through its elements
            a.add(j) #put it in the set, so we know what to ignore later

for i in lists:
    if len(i) > 1:
        ans.append(i) #if there's more than 1 element, we should add it to the final answer
    else:
        if i[0] not in a:
            ans.append(i) #append if it does not appear in the set

lists = ans #copy the answer list to the original (optional step)
print(lists) 

This gives the desired output:

[[2], [3], [4, 5, 6], [7], [8], [9, 10, 11]]

Of course, if you don't care about readability, you could use the one-liner:

lists = list(filter(lambda i: len(i) > 1 or not (any(i[0] in sb for sb in list(filter(lambda i: len(i) > 1, lists)))), lists))

This gives the same correct output.

  • Related