Skip to content

Instantly share code, notes, and snippets.

@superatomic
Created May 28, 2023 19:43
Show Gist options
  • Save superatomic/65ba271f8ab5d1a2f966dcc4f9e458ae to your computer and use it in GitHub Desktop.
Save superatomic/65ba271f8ab5d1a2f966dcc4f9e458ae to your computer and use it in GitHub Desktop.
A python 3.12 `unique` function, with type hints.
from collections.abc import Hashable, Iterable, Iterator
def unique[T: Hashable](items: Iterable[T]) -> Iterator[T]:
seen = set()
for item in items:
if item not in seen:
seen.add(item)
yield item
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment