Nested lists, simple though

sturlamolden sturlamolden at yahoo.no
Sun Apr 20 20:35:31 EDT 2008


On Apr 21, 12:25 am, Zethex <zet... at hotmail.com> wrote:

> Anyway the amount of [[]] do increase over time.

You can flatten a nested list using a closure and recursion:

def flatten(lst):
    tmp = []
    def _flatten(lst):
        for elem in lst:
            if type(elem) != list:
                tmp.append(elem)
            else:
                _flatten(elem)
    _flatten(lst)
    return tmp


However, CPython does not recurse very fast, but Cython and Pyrex do.
First, get rid of the closure, it is not supported in Cython or Pyrex
(at least not yet). Then change the recursive function to a cdef, i.e.
a normal C function which is called without the overhead of Python's
attribute lookup:

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

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

Compile this with Cython or Pyrex, and you get a very fast nested list
flattener.

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






More information about the Python-list mailing list