Well, since set
s inherently dedupe things, your first instinct might be to do set(mylist)
. However, that doesn't quite work:
In [1]: mylist = [[1,2,3], ['a', 'c'], [3,4,5],[1,2], [3,4,5], ['a', 'c'], [3,4,5], [1,2]]
In [2]: set(mylist)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-b352bcae5975> in <module>()
----> 1 set(mylist)
TypeError: unhashable type: 'list'
This is because set
s only work on iterable
s of hashable elements (and since list
s are mutable, they are not hashable).
Instead, you can do this simply for the price of converting your sublists to subtuples:
In [3]: set([tuple(x) for x in mylist])
Out[3]: {(1, 2), (1, 2, 3), (3, 4, 5), ('a', 'c')}
Or, if you really need a list of lists again:
In [4]: [list(x) for x in set([tuple(x) for x in mylist])]
Out[4]: [[1, 2], [3, 4, 5], ['a', 'c'], [1, 2, 3]]