Skip to content

Instantly share code, notes, and snippets.

@aljiwala
Last active December 23, 2016 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aljiwala/c77a01f382f5bbc10d2d2b97a7ed0f0a to your computer and use it in GitHub Desktop.
Save aljiwala/c77a01f382f5bbc10d2d2b97a7ed0f0a to your computer and use it in GitHub Desktop.
Traverse Generator Function in Python
def traverse(o, tree_types=(list, tuple)):
if isinstance(o, tree_types):
for value in o:
for subvalue in traverse(value, tree_types):
yield subvalue
else:
yield o
data = [(1,1,(1,1,(1,"1"))),(1,1,1),(1,),1,(1,(1,("1",)))]
print list(traverse(data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment