Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I have duplicates in a list with brackets, what should I do

Tags:

Suppose I have the following list:

 m=[1,2,[1],1,2,[1]]

I wish to take away all duplicates. If it were not for the brackets inside the the list, then I could use:

 m=list(set(m))

but when I do this, I get the error:

unhashable type 'set'.

What command will help me remove duplicates so that I could only be left with the list

 m=[1,2,[1]]

Thank you

like image 773
Stiven G Avatar asked Aug 27 '18 01:08

Stiven G


2 Answers

You can do something along these lines:

m=[1,2,[1],1,2,[1]]
seen=set()
nm=[]
for e in m:
    try:
        x={e}
        x=e
    except TypeError:
        x=frozenset(e)  
    if x not in seen:
        seen.add(x) 
        nm.append(e)
>>> nm
[1, 2, [1]]

From comments: This method preserves the order of the original list. If you want the numeric types in order first and the other types second, you can do:

sorted(nm, key=lambda e: 0 if isinstance(e, (int,float)) else 1)
like image 52
dawg Avatar answered Oct 11 '22 17:10

dawg


The first step will be to convert the inner lists to tuples:

>> new_list = [tuple(i) if type(i) == list else i for i in m]

Then create a set to remove duplicates:

>> no_duplicates = set(new_list)
>> no_duplicates
{1, 2, (1,)}

and you can convert that into list if you wish.

like image 35
Zadiq Avatar answered Oct 11 '22 16:10

Zadiq