great-ai/good_ai/src/sus/unique.py
András Schmelczer d79a75d352 Add files
Signed-off-by: András Schmelczer <andras@schmelczer.dev>
2022-04-02 13:47:33 +02:00

16 lines
483 B
Python

from typing import Any, Callable, Iterable, List
def unique(
values: Iterable[Any], *, key: Callable[[Any], Any] = lambda v: v
) -> List[Any]:
"""Only keep first occurrences and maintian order"""
key_values = {}
for v in values:
k = key(v)
if k not in key_values:
# dicts maintin insertion order: https://mail.python.org/pipermail/python-dev/2017-December/151283.html
key_values[k] = v
return list(key_values.values())