0

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]]
8
  • What would you do about something like [[2, 3], [3, 4], [4, 5]]? Commented Jul 6, 2022 at 16:20
  • how many levels of nesting are we talking about? Commented Jul 6, 2022 at 16:36
  • in your example you find 9 in [9] and in [9,10,11] , the same with 10. So do you want the list with longer length to be retained? Commented Jul 6, 2022 at 16:39
  • @RyanZhang There would possibly be [[2],[2,3],[4],[4,5]]. Only a single element would be duplicated with one in other list of element. Commented Jul 6, 2022 at 16:42
  • @RinkeshP Yes. That's what i hope for. Commented Jul 6, 2022 at 16:43

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.