Created
June 29, 2022 06:45
-
-
Save meadsteve/9e98f7b0a11129de1c8f3a30f2acc35d to your computer and use it in GitHub Desktop.
Iterate over unique items in a python iterable
This file contains hidden or 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
from typing import Iterable, Hashable | |
def iterate_uniquely(items: Iterable[Hashable]): | |
already_emitted = set() | |
for item in items: | |
key = hash(item) | |
if key not in already_emitted: | |
yield item | |
already_emitted.add(key) | |
assert list(iterate_uniquely([1, 2, 3])) == [1, 2, 3] | |
assert list(iterate_uniquely([1, 3, 2, 3])) == [1, 3, 2] | |
assert list(iterate_uniquely([(1, 2), (1, 3), (2, 1)])) == [(1, 2), (1, 3), (2, 1)] | |
assert list(iterate_uniquely([(1, 2), (1, 3), (2, 1), (2, 1)])) == [(1, 2), (1, 3), (2, 1)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment