summaryrefslogtreecommitdiff
path: root/venv/lib/python3.11/site-packages/litestar/utils/sequence.py
blob: 01ef1a813be75cb10fffdfe26083794ae3d4e7e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from __future__ import annotations

from typing import Callable, Sequence, TypeVar

__all__ = ("find_index", "unique")


T = TypeVar("T")


def find_index(target_list: Sequence[T], predicate: Callable[[T], bool]) -> int:
    """Find element in list given a key and value.

    List elements can be dicts or classes
    """
    return next((i for i, element in enumerate(target_list) if predicate(element)), -1)


def unique(value: Sequence[T]) -> list[T]:
    """Return all unique values in a given sequence or iterator."""
    try:
        return list(set(value))
    except TypeError:
        output: list[T] = []
        for element in value:
            if element not in output:
                output.append(element)
        return output