Last active
December 1, 2020 05:07
-
-
Save fogleman/8937897 to your computer and use it in GitHub Desktop.
Python Unique / Distinct Elements Iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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