Skip to content

Instantly share code, notes, and snippets.

@micahscopes
Last active February 3, 2016 06:39
Show Gist options
  • Save micahscopes/06f598fc1e4650336ba8 to your computer and use it in GitHub Desktop.
Save micahscopes/06f598fc1e4650336ba8 to your computer and use it in GitHub Desktop.
flatten nested lists/tuples
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