stouputils.collections.iterable module#
- unique_list(
- list_to_clean: Iterable,
- method: Literal['id', 'hash', 'str'] = 'str',
Remove duplicates from the list while keeping the order using ids, hash, or str
- Parameters:
list_to_clean (Iterable[T]) – The list to clean
method (Literal["id", "hash", "str"]) – The method to use to identify duplicates
- Returns:
The cleaned list
- Return type:
list[T]
Examples
>>> unique_list([1, 2, 3, 2, 1], method="id") [1, 2, 3]
>>> s1 = {1, 2, 3} >>> s2 = {2, 3, 4} >>> s3 = {1, 2, 3} >>> unique_list([s1, s2, s1, s1, s3, s2, s3], method="id") [{1, 2, 3}, {2, 3, 4}, {1, 2, 3}]
>>> s1 = {1, 2, 3} >>> s2 = {2, 3, 4} >>> s3 = {1, 2, 3} >>> unique_list([s1, s2, s1, s1, s3, s2, s3], method="str") [{1, 2, 3}, {2, 3, 4}]
- at_least_n(
- iterable: Iterable,
- predicate: Callable[[T], bool],
- n: int,
Return True if at least n elements in iterable satisfy predicate. It’s like the built-in any() but for at least n matches.
Stops iterating as soon as n matches are found (short-circuit evaluation).
- Parameters:
iterable (Iterable[T]) – The iterable to check.
predicate (Callable[[T], bool]) – The predicate to apply to items.
n (int) – Minimum number of matches required.
- Returns:
True if at least n elements satisfy predicate, otherwise False.
- Return type:
bool
Examples
>>> at_least_n([1, 2, 3, 4, *[i for i in range(5, int(1e5))]], lambda x: x % 2 == 0, 2) True >>> at_least_n([1, 3, 5, 7], lambda x: x % 2 == 0, 1) False