Python eliminates duplicate list with immiscible items on one line

Possible duplicate:
Python: remove duplicates from a list of lists

Say I have a list

a=[1,2,1,2,1,3]

If all the elements in hashed (as in this case), this will complete the task:

list(set(a))

But, what if

a=[[1,2],[1,2],[1,3]]

?

+5
source share
2 answers
>>> from itertools import groupby
>>> a = [[1,2],[1,2],[1,3]]
>>> [k for k,v in groupby(sorted(a))]
[[1, 2], [1, 3]]
+10
source

This installation understanding works for a list of lists for creating a tuple set:

>>> {(tuple(e)) for e in a}
set([(1, 2), (1, 3)])

Then use this to include it again in the list of lists without duplicates:

>>> [list(x) for x in {(tuple(e)) for e in a}]
[[1, 2], [1, 3]]
0
source

All Articles