Removing duplicates from a list of lists

前端 未结 12 1498
萌比男神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 10:53

    Create a dictionary with tuple as the key, and print the keys.

    • create dictionary with tuple as key and index as value
    • print list of keys of dictionary

    k = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]]
    
    dict_tuple = {tuple(item): index for index, item in enumerate(k)}
    
    print [list(itm) for itm in dict_tuple.keys()]
    
    # prints [[1, 2], [5, 6, 2], [3], [4]]
    

提交回复
热议问题