Is there a Python module that already does this?

Emile van Sebille emile at fenx.com
Wed Feb 6 11:53:54 EST 2002


"MDK" <mdk at mdk.com> wrote in message
news:a3rbad$19u08k$1 at ID-98166.news.dfncis.de...
> I need to convert a list into a list of characters.
>
> For example:
>
> ("cat",5,['dog',[3,3,1]],"zoo")
>
> Would become:
>
> ('c','a','t',5,'d','o','g',3,3,1,'z','o','o')
>

def flatten(ary, rslt):
    try:
        flatten(ary[0], rslt)
        flatten(ary[1:], rslt)
    except:
        if ary: rslt.append(ary)

rslt = []
flatten(("cat",5,['dog',[3,3,1]],"zoo"), rslt)
print 'rslt = ',tuple(rslt)

HTH,

--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list