Last active
December 23, 2016 09:46
-
-
Save aljiwala/c77a01f382f5bbc10d2d2b97a7ed0f0a to your computer and use it in GitHub Desktop.
Traverse Generator Function in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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