index nested lists
Jean-Michel Pichavant
jeanmichel at sequans.com
Tue Jul 28 09:39:56 EDT 2009
Alex wrote:
> 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
>
One hidden suggestion in Andreas answer is to write your code in
english, if you can :o)
JM
More information about the Python-list
mailing list