Removing duplicates from a list of lists

前端 未结 12 1277
萌比男神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:16

    All the set-related solutions to this problem thus far require creating an entire set before iteration.

    It is possible to make this lazy, and at the same time preserve order, by iterating the list of lists and adding to a "seen" set. Then only yield a list if it is not found in this tracker set.

    This unique_everseen recipe is available in the itertools docs. It's also available in the 3rd party toolz library:

    from toolz import unique
    
    k = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]]
    
    # lazy iterator
    res = map(list, unique(map(tuple, k)))
    
    print(list(res))
    
    [[1, 2], [4], [5, 6, 2], [3]]
    

    Note that tuple conversion is necessary because lists are not hashable.

提交回复
热议问题