counting items

Bernhard Herzog bh at intevation.de
Wed Jan 12 14:10:38 EST 2005


"It's me" <itsme at yahoo.com> writes:

> May be flatten should be build into the language somehow....

That shouldn't be necessary as it can easily be written in a single list
comprehension:

a = [[1,2,4],4,5,[2,3]]
flat_a = [x for cur, rest in [[a[:1], a[1:]]] for x in cur
            if (not isinstance(x, (list, tuple))
                and (not rest or not cur.append(rest.pop(0)))
                or (x and (cur.append(x[0]) or rest.__setslice__(0, 0, x[1:])))
                or (not x and rest and cur.append(rest.pop(0))))]

;-)

   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Skencil                                           http://skencil.org/
Thuban                                  http://thuban.intevation.org/



More information about the Python-list mailing list