Skip to content

Instantly share code, notes, and snippets.

@germn
germn / unique_everseen.py
Created November 7, 2015 08:19
Universal function to return unique items in iterable
def unique_everseen(items: Iterable, get_hashable: Callable[[Any], Any]=None) -> Iterable:
"""
Returns generator of unique items.
If get_hashable(item) is not None or all elements hashable, fast method would be used.
"""
if callable(get_hashable):
seen = set()
return (i for i in items for h in (get_hashable(i),) if not (h in seen or seen.add(h)))
elif all(isinstance(i, collections.Hashable) for i in items):
seen = set()