Skip to content

edsnlp.utils.lists

flatten(my_list)

Flatten (if necessary) a list of sublists

PARAMETER DESCRIPTION
my_list

A list of items, each items in turn can be a list

TYPE: List

RETURNS DESCRIPTION
List

A flatten list

Source code in edsnlp/utils/lists.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def flatten(my_list: List):
    """
    Flatten (if necessary) a list of sublists

    Parameters
    ----------
    my_list : List
        A list of items, each items in turn can be a list

    Returns
    -------
    List
        A flatten list
    """
    if not my_list:
        return my_list
    my_list = [item if isinstance(item, list) else [item] for item in my_list]

    return [item for sublist in my_list for item in sublist]