Skip to content

Instantly share code, notes, and snippets.

@meadsteve
Created June 29, 2022 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meadsteve/9e98f7b0a11129de1c8f3a30f2acc35d to your computer and use it in GitHub Desktop.
Save meadsteve/9e98f7b0a11129de1c8f3a30f2acc35d to your computer and use it in GitHub Desktop.
Iterate over unique items in a python iterable
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