0

Our example list:

(
    [(), (2, 0)], 
    [(2,), (0,)], 
    [(), (0, 2)], 
    [(0,), (2,)]
)

I want to be able to remove duplicates in the list, and by this I mean same inside elements.

This means the elements [(2,), (0,)] and [(0,), (2,)] in the list are same. So in summary, i want to remove different order inside elements. Note that this example is of 2 inside elements but I want it for any number. I want to keep whatever comes first as long as they are not both (or a many duplicates we have) there.

I thought about sorting the inner elements, converting to str them checking for duplicates, but I am not sure if this is the way and I don't know how to make it

For example an element of [(),(2,0),(1,)] is the same as [(),(1,),(2,0)].

5
  • Is your list a list of str types, or of list types? Not sure if a typo by you, or intentional. Commented Mar 17, 2020 at 20:37
  • I edited the list to remove the str of elements. The outer one is list Commented Mar 17, 2020 at 20:40
  • 2
    The fact that you have 3 levels of list nesting makes this a bit trickier. Is [(), (0,2), (1,)] the same as [(), (2,0),(1,)]? Commented Mar 17, 2020 at 20:56
  • it is important the order of elements(lists) after removing the duplicates? Commented Mar 17, 2020 at 21:00
  • Does this answer your question? Python: Remove Sublists from List if Same Including/Not Including Order Commented Mar 17, 2020 at 21:10

2 Answers 2

2

you could use fozensets:

l = (
    [(), (2, 0)], 
    [(2,), (0,)], 
    [(), (0, 2)], 
    [(0,), (2,)]
)


r = set()

result = tuple()
for e in l:
    f = frozenset(e)
    if f not in r:
        result += (e,)
        r.add(f)
result

output:

([(), (2, 0)], [(2,), (0,)], [(), (0, 2)]

if the order of the lists is not important you can use:

tuple(list(e) for e in {frozenset(e) for e in l})

output:

([(), (0, 2)], [(2,), (0,)], [(2, 0), ()])
Sign up to request clarification or add additional context in comments.

2 Comments

@Todd no, the elements from the tuples inside of the lists are not allowed to change the order, only the tuples inside lists are allowed to change the order
You do the same using sorted(e) and result as a list. But with frozenset it's little bit faster. Good answer by the way.
0

One way to solve this, is to sort the values in the list and remove check if the nth element exists in the next part of the list

from itertools import compress

lst = (
    [(), (2, 0)], 
    [(2,), (0,)], 
    [(), (0, 2)], 
    [(0,), (2,)]
)

_ = list(map(lambda x: x.sort(), lst))
list(compress(lst, [x not in lst[i+1:] for i, x in enumerate(lst)]))

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.