libb.unique_iter
- unique_iter(iterable, key=None)
Yield unique elements, preserving order.
>>> list(unique_everseen('AAAABBBCCDAABBB')) ['A', 'B', 'C', 'D'] >>> list(unique_everseen('ABBCcAD', str.lower)) ['A', 'B', 'C', 'D']
Sequences with a mix of hashable and unhashable items can be used. The function will be slower (i.e., O(n^2)) for unhashable items.
Remember that
listobjects are unhashable - you can use the key parameter to transform the list to a tuple (which is hashable) to avoid a slowdown.>>> iterable = ([1, 2], [2, 3], [1, 2]) >>> list(unique_everseen(iterable)) # Slow [[1, 2], [2, 3]] >>> list(unique_everseen(iterable, key=tuple)) # Faster [[1, 2], [2, 3]]
Similarly, you may want to convert unhashable
setobjects withkey=frozenset. Fordictobjects,key=lambda x: frozenset(x.items())can be used.