def flatten(L):
    """Flatten a list recursively

    Inspired by this fun discussion: https://stackoverflow.com/questions/12472338/flattening-a-list-recursively

    np.array.flatten did not work for irregular arrays
    and itertools.chain.from_iterable cannot handle arbitrarily nested lists

    :param L: A list to flatten
    :return: the flattened list
    """
    if L == []:
        return L
    if isinstance(L[0], list):
        return flatten(L[0]) + flatten(L[1:])
    return L[:1] + flatten(L[1:])