"Devan L" wrote:

> def flatten(iterable):
>     if not hasattr(iterable, '__iter__'):
>         return [iterable]
>     return sum([flatten(element) for element in iterable],[])
> Recursion makes things so much shorter.

The last line can faster and more compact by:

from itertools import imap

def flatten(iterable):
    if not hasattr(iterable, '__iter__'):
        return [iterable]
    return sum(imap(flatten,iterable),[])

George

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to