index nested lists

Alex magobin at gmail.com
Tue Jul 28 09:19:03 EDT 2009


On 28 Lug, 15:12, "Andreas Tawn" <andreas.t... at ubisoft.com> wrote:
> > hi at all,
> >  If I have this list:
>
> > >>> lista
> > ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]]
>
> > if I want enumerate elements...I can see:
>
> > >>> for parola in lista:
> >    print lista[i]
> >    i = i + 1
>
> > ciao
> > 1
> > ['mela', 'pera', 'banana']
> > [1, 2, 3]
>
> > but, if I want to enumerate elements about nested lists ??, something
> > like:
>
> > ciao
> > 1
> > mela
> > pera
> > banana
> > 1
> > 2
> > 3
>
> > ...How can I do ??
>
> > Alex
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
> You could do something like this.
>
> def printNestedList(lst):
>     if isinstance(lst, list):
>         for element in lst:
>             printNestedList(element)
>     else:
>         print lst
>
> myList = ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]]
> printNestedList(myList)
>


thanks a lot !

Alex



More information about the Python-list mailing list