File size: 903 Bytes
5b76e0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
from __future__ import annotations

from typing import Callable, Iterable, TypeVar


T = TypeVar("T")


def partition(
    pred: Callable[[T], object], iterable: Iterable[T]
) -> tuple[list[T], list[T]]:
    """Partition a sequence in to two list from a given predicate. The first list will contain
    the values where the predicate is False, the second list will contain the remaining values.

    Args:
        pred (Callable[[T], object]): A callable that returns True or False for a given value.
        iterable (Iterable[T]): In Iterable of values.

    Returns:
        tuple[list[T], list[T]]: A list of values where the predicate is False, and a list
            where the predicate is True.
    """

    result: tuple[list[T], list[T]] = ([], [])
    appends = (result[0].append, result[1].append)

    for value in iterable:
        appends[1 if pred(value) else 0](value)
    return result