Rene Fyrno
Rene Fyrno

Reputation: 11

In python, how to check if a list of lists has duplicates?

I searched for the answer to my question and found that many said to try set(my_list), but in my case (I have lists within a list) python throws an error:

Traceback (most recent call last):
  File "E:/Users/Rene/PycharmProjects/vaip/Main.py", line 17, in <module>
  set(punktid)
TypeError: unhashable type: 'list'

So my question is, how can I find and remove duplicates in a list like this:

my_list = [[0, 3], [3, 4], [5, 6], [9, 2], [0, 3]]

Upvotes: 1

Views: 312

Answers (2)

vaultah
vaultah

Reputation: 46523

Converting all sublists to tuples makes it possible to create set (all elements become hashable):

>>> {tuple(x) for x in my_list}
{(5, 6), (9, 2), (0, 3), (3, 4)}

In this case we use braces to denote a set comprehension, though it's also possible to use ordinary set constructor:

>>> set(tuple(x) for x in my_list)
{(5, 6), (9, 2), (0, 3), (3, 4)}

If your final result must be a list of lists (the order may not be preserved):

>>> [list(y) for y in {tuple(x) for x in my_list}]
[[5, 6], [9, 2], [0, 3], [3, 4]]

Upvotes: 3

Netro
Netro

Reputation: 7297

You can use,

>>> l
[[0, 3], [3, 4], [5, 6], [9, 2], [0, 3]]
>>> def remove_duplicate(l):
    tup = [tuple(x) for x in l]
    set_tup = set(tup)
    return [list(x) for x in list(set_tup) ]

>>> print remove_duplicate(l)
[[5, 6], [9, 2], [0, 3], [3, 4]]

Upvotes: 0

Related Questions