0

Say I have an list list of tuples like this:

a = [(["I", "love", "apples"], "good"),
(["I", "love", "apples"], "good")),
(["I", "hate", "apples"], "bad")),
(["I", "dislike", "apples"], "bad")),]

I want to remove duplicated, but list(set(a))errors out:

TypeError: unhashable type: 'list'

Is there any pythonic way to remove duplicates from this type of list

user1452494
  • 1,145
  • 5
  • 18
  • 40

2 Answers2

2

Assuming you are creating the data, use tuples as the first item instead of lists

a = [
    (("I", "love", "apples"), "good"),
    (("I", "love", "apples"), "good"),
    (("I", "hate", "apples"), "bad"),
    (("I", "dislike", "apples"), "bad")
]

print len(set(a))

Output:

3
Ngenator
  • 10,909
  • 4
  • 41
  • 46
1

lists are not hashable so you can not simple call set on a.

you can convert the inner lists to tuples, then call list(set()):

list(set([(tuple(sent), good_bad) for sent, good_bad in a]))
Jeff Tsui
  • 1,266
  • 12
  • 20