Iterating through a nested list

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sun Apr 16 03:27:34 EDT 2006


In <pan.2006.04.16.04.34.55.395137 at REMOVETHIScyber.com.au>, Steven
D'Aprano wrote:

> for obj in Array:
>     if isinstance(obj, list):
>         for item in obj:
>             print item
>     else:
>         print obj

I would "reverse" that check and test `obj` for not being a string::

 for obj in Array:
     if not isinstance(obj, basestring):
         for item in obj:
             print item
     else:
         print obj

This way it works with other types of iterables than lists too.  Often
strings are the only type that one don't want to iterate over in such a
context.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list