Skip to content

list_utils

Library for list utility functions

flatten_list(_2d_list)

Flattens list of lists to one list.

Parameters:

Name Type Description Default
_2d_list list

list to be flattened

required

Returns:

Type Description
list

flat list

Source code in utils/list_utils.py
def flatten_list(_2d_list: list[Any]) -> list:
    """Flattens list of lists to one list.

    Args:
        _2d_list (list): list to be flattened

    Returns:
        list: flat list
    """
    flat_list = []
    # Iterate through the outer list
    for element in _2d_list:
        if isinstance(element, list):
            flat_list = flat_list + list(element)
        else:
            flat_list.append(element)

    return flat_list

items_in(first, second)

Returns a list of items in the first list that are in the second list.

Parameters:

Name Type Description Default
first List[str]

list of items to iterate

required
second List[str]

list of items to check

required

Returns:

Type Description
List[str]

list of items that were in second list

Source code in utils/list_utils.py
def items_in(first: List[str], second: List[str]) -> List[str]:
    """Returns a list of items in the first list that are in the second list.

    Args:
        first (List[str]): list of items to iterate
        second (List[str]): list of items to check

    Returns:
        List[str]: list of items that were in second list
    """
    return list(filter(lambda var: var in second, first))

items_not_in(first, second)

Returns a list of items in the first list that are not in the second list.

Parameters:

Name Type Description Default
first List[str]

list of items to iterate

required
second List[str]

list of items to check

required

Returns:

Type Description
List[str]

list of items that were not in second list

Source code in utils/list_utils.py
def items_not_in(first: List[str], second: List[str]) -> List[str]:
    """Returns a list of items in the first list that are not in the second list.

    Args:
        first (List[str]): list of items to iterate
        second (List[str]): list of items to check

    Returns:
        List[str]: list of items that were not in second list
    """
    return list(filter(lambda var: var not in second, first))

remove_duplicates_from_list(list_with_duplicates)

Removes duplicates from list.

Parameters:

Name Type Description Default
list list

list to be made distinct

required

Returns:

Type Description
list

list without duplicates

Source code in utils/list_utils.py
def remove_duplicates_from_list(list_with_duplicates: list) -> list:
    """Removes duplicates from list.

    Args:
        list (list): list to be made distinct

    Returns:
        list: list without duplicates
    """

    return list(set(list_with_duplicates))