1

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]]
1
  • This question might help to understand the whole hashing thing: Hashing question Commented Nov 16, 2014 at 14:38

2 Answers 2

3

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]]
Sign up to request clarification or add additional context in comments.

1 Comment

Might be worth pointing out that {...} instantiates the stuff inside as a set.
0

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]]

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.