Removing duplicates from a list of lists

前端 未结 12 1437
萌比男神i
萌比男神i 2020-11-22 10:37

I have a list of lists in Python:

k = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]]

And I want to remove duplicate elements from it. Was if it

12条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 11:19

    k=[[1, 2], [4], [5, 6, 2], [1, 2], [3], [5, 2], [3], [8], [9]]
    kl=[]
    kl.extend(x for x in k if x not in kl)
    k=list(kl)
    print(k)
    

    which prints,

    [[1, 2], [4], [5, 6, 2], [3], [5, 2], [8], [9]]
    

提交回复
热议问题