def flatten(seq, scalarp=is_scalar_or_string):
for item in seq:<EOL><INDENT>if scalarp(item): yield item<EOL>else:<EOL><INDENT>for subitem in flatten(item, scalarp):<EOL><INDENT>yield subitem<EOL><DEDENT><DEDENT><DEDENT>
this generator flattens nested containers such as

>>> l=( ('John', 'Hunter'), (1,23), [[[[42,(5,23)]]]])

so that

>>> for i in flatten(l): print i,
John Hunter 1 23 42 5 23

By: Composite of Holger Krekel and Luther Blissett
From: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/121294
and Recipe 1.12 in cookbook