Nested lists, simple though

sturlamolden sturlamolden at yahoo.no
Sun Apr 20 20:48:09 EDT 2008


On Apr 21, 2:35 am, sturlamolden <sturlamol... at yahoo.no> wrote:

> This also shows how easy it is to boost the performance of Python code
> using Cython.

We can improve this further by getting rid of the tmp.append attribue
lookup:

cdef _flatten(lst, append):
    for elem in lst:
        if type(elem) != list:
            append(elem)
        else:
            _flatten(elem, append)

def flatten(lst):
    tmp = []
    _flatten(lst, tmp.append)
    return tmp




More information about the Python-list mailing list