counting items

Craig Ringer craig at postnewspapers.com.au
Thu Jan 13 14:45:27 EST 2005


On Wed, 2005-01-12 at 20:10 +0100, Bernhard Herzog wrote:
> "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))))]
> 
> ;-)

If it means I _never_ have to see that list comprehension again, then
seeing 'flatten' go into itertools would make me very, very happy :-P

-- 
Craig Ringer




More information about the Python-list mailing list