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
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.