Last active
February 3, 2016 06:39
-
-
Save micahscopes/06f598fc1e4650336ba8 to your computer and use it in GitHub Desktop.
flatten nested lists/tuples
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
squash = lambda l: reduce(lambda x,y: x+y,map(lambda e: squash(e) if isinstance(e,(list,tuple)) else [e], l), []) | |
squash([1,2,(3,[4,5])]) # => [1,2,3,4,5] | |
# an real world example ;) | |
"\n".join(squash([header,body,footer])) | |
# => <html>\n<body><h1>Hello World!</h1></body>\n</html> | |
# observe that the sections could be any kind of mess of nested arrays. | |
# but in the end, all we really care about is that they get strung together |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment