Iterating through a nested list
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Sun Apr 16 00:34:56 EDT 2006
On Sat, 15 Apr 2006 22:36:17 -0500, CamelR wrote:
> I have a newbie question, and I have seen reference to this mentioned
> in many places, but all just say "this has been discussed frequently
> in (other places) so we won't discuss it here..." and I am unable to
> find the actual answer...
>
> I expected :
>
> Array = ["topdir", ["/subdir1", ["/file1", "/file2"], "/subdir2",
> ["/file1", "/file2"], "/subdir3", ["/file1", "/file2"]]]
>
> for i in range(len(Array)):
> print Array[i]
> for x in range(len(Array[i])):
> print Array[i][x]
> for y in range(len(Array[i][x])):
> print Array[i][x][y]
If you don't actually need the index of each item, don't use for i in
range(), just iterate over the list:
for obj in Array:
print obj
Notice that your list has two items, the first is a string and the second
is a sub-list. The natural way of proceeding would be:
for obj in Array:
for item in obj:
print item
but that gives too much: it iterates over the string, giving you
individual characters.
Instead, do this:
for obj in Array:
if isinstance(obj, list):
for item in obj:
print item
else:
print obj
which helps, but not enough: it only prints two levels down, and your data
has three. This time -- next time it might have two, or fifteen, or one...
Instead, create a helper function like this:
def walk(seq):
"""Walk over a sequence of items, printing each one in turn, and
recursively walking over sub-sequences.
"""
print seq
if isinstance(seq, list):
for item in seq:
walk(item)
Notice that this function calls itself.
This version of walk() prints the entire sequence at every level. Here is
a version which doesn't:
def walk2(seq):
if isinstance(seq, list):
for item in seq:
walk2(item)
else:
print seq
--
Steven.
More information about the Python-list
mailing list