How to remove duplicate tuples from a list in python?

前端 未结 4 2032
伪装坚强ぢ
伪装坚强ぢ 2021-01-03 11:22

I have a list that contains list of tuples as follows.

mylist = [[\'xxx\', 879], [\'yyy\', 315], [\'xxx\', 879], [\'zzz\', 171], [\'yyy\', 315]]
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-03 12:00

    The reason that you're not able to do this is because you have a list of lists and not a list of tuples.

    What you could do is:

    mytuplelist = [tuple(item) for item in mylist]
    mylist = list(set(mytuplelist))
    

    or

    mylist = list(set(map(tuple, mylist)))
    

提交回复
热议问题