Skip to content

collections_utils

cut_iterable_to(obj, to)

Cut an iterable to a certain length.

Parameters:

Name Type Description Default
obj Iterable

The iterable to cut.

required
to int

The number of elements to return.

required

Returns:

Type Description
Tuple[Union[List, Mapping], int]

A list or dictionary (depending on the type of object) of elements and the remaining elements in the original list or dictionary.

Source code in emodpy/utils/collections_utils.py
def cut_iterable_to(obj: Iterable, to: int) -> Tuple[Union[List, Mapping], int]:
    """
    Cut an iterable to a certain length.

    Args:
        obj (Iterable): The iterable to cut.
        to (int): The number of elements to return.

    Returns:
        (Tuple[Union[List, Mapping], int]): A list or dictionary (depending on the type of object) of elements and
            the remaining elements in the original list or dictionary.
    """
    if isinstance(obj, dict):
        slice = {k: v for (k, v) in take(to, obj.items())}
    else:
        slice = take(to, obj)

    remaining = len(obj) - to
    remaining = 0 if remaining < 0 else remaining
    return slice, remaining