Files
payphone/payphone/core/utils.py
T
2024-10-19 22:26:36 -03:00

29 lines
614 B
Python

from typing import Callable, Iterable, TypeVar
T = TypeVar("T")
U = TypeVar("U")
def deduplicate(i: Iterable[T], key: Callable[[T], U] = lambda i: i) -> list[T]:
"""Deduplicates an Iterable.
Args:
i (Iterable[T]): The iterable with items to deduplicate.
key (Callable[[T], U], optional): The key by which to deduplicate. Defaults to
`lambda i: i`.
Returns:
list[T]: A list with the deduplicated items.
"""
found_keys = set()
result = []
for item in i:
if key(item) not in found_keys:
result.append(item)
return result