Skip to content

Instantly share code, notes, and snippets.

@fogleman
Last active December 1, 2020 05:07
Show Gist options
  • Save fogleman/8937897 to your computer and use it in GitHub Desktop.
Save fogleman/8937897 to your computer and use it in GitHub Desktop.
Python Unique / Distinct Elements Iterator
def distinct(iterable, keyfunc=None):
seen = set()
for item in iterable:
key = item if keyfunc is None else keyfunc(item)
if key not in seen:
seen.add(key)
yield item
if __name__ == '__main__':
x = [0, 0, 1, 0, 1, 2, 2, 1, 0]
assert list(distinct(x)) == [0, 1, 2]
x = ['', 'a', 'abc', 'cat', 'dog', 'hi']
assert list(distinct(x, len)) == ['', 'a', 'abc', 'hi']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment