disgrating a list

Gimble marleyziggy at hotmail.com
Sat Sep 2 02:21:41 CEST 2006

Or, (to add needed recursion to your simple loop):

def flatten(x):
    "Modified in-place flattening"
    for i in range(len(x)):
        if isinstance(x[i], list):
            x[i:i+1] = flatten(x[i])
    return x

This version modifies in-place (as yours does), which may or may not be
what you want, depending on whether or not you need both the flat and
un-flat versions.

More information about the Python-list mailing list