Possible Duplicate:
Flatten (an irregular) list of lists in Python
I wanted a solution to list/print all the items in a nested list (with arbitrary nesting level). Here is what I came up with:
items = []
def getitems(mylist):
for item in mylist:
if type(item) is list:
getitems(item)
else:
items.append(item)
return items
Sample output:
foo=['foo','bar',['foo','bar','baz'],'spam',['ham','eggs','salami']]
In [8]: getitems(foo)
Out[8]:
['foo',
'bar',
'foo',
'bar',
'foo',
'bar',
'baz',
'spam',
'ham',
'eggs',
'salami']
Is this a good solution? Or is there a better approach?