index nested lists

Andreas Tawn andreas.tawn at ubisoft.com
Tue Jul 28 15:12:41 CEST 2009


> 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)

>>>
ciao
1
mela
pera
banana
1
2
3

Cheers, Drea



More information about the Python-list mailing list