Python and lists:flatten. Such as in Erlang

Python does not has function to transform nested lists in a flatten lists, such as lists:flatten in Erlang. This is my example.

Erlang:


1> L = [1,[2,[4,4,4,[5,5,5,5]],2,2,2,[3,3,3,3]],3,[4,5,6],[7,[8,[9,10]]]].
2> lists:flatten(L).
[1, 2, 4, 4, 4, 5, 5, 5, 5, 2, 2, 2, 3, 3, 3, 3, 3, 4, 5, 6, 7, 8, 9, 10]
3>

Python:


def flat_list(ls,new_ls=[]):
    for i in ls:
        if type(i) == list:
            flat_list(i,new_ls)
        else:
            new_ls.append(i)
    return new_ls

>>> ls = [1, [2, [4, 4, 4, [5, 5, 5, 5]], 2, 2, 2, [3, 3, 3, 3]], 3, [4, 5, 6], [7, [8, [9, 10]]]]
>>> flat_list(ls)
[1, 2, 4, 4, 4, 5, 5, 5, 5, 2, 2, 2, 3, 3, 3, 3, 3, 4, 5, 6, 7, 8, 9, 10]





coded by nessus