flatten()

Björn Lindström bkhl at stp.ling.uu.se
Thu Jul 7 13:10:01 EDT 2005


Tom Anderson <twic at urchin.earth.li> writes:

> Stian Søiland wrote:
>
> > Or what about a recursive generator?
>
> That's the sort of thing i like to see!

That can be a neat method. It's a pretty verbose way to do flatten(),
but it's a good example:

def flatten(l):
    for e in l:
        if isinstance(e, list):
            for f in flatten(e):
                yield f
        else:
            yield e

for x in flatten([0, [1, 2, [3, 4], 5], 6, 7]):
    whatever()

--
Björn Lindström <bkhl at stp.ling.uu.se>
Student of computational linguistics, Uppsala University, Sweden



More information about the Python-list mailing list