Skip to content

Instantly share code, notes, and snippets.

@pranavgarg
Created October 28, 2016 18:34
Show Gist options
  • Save pranavgarg/aa72787152c436275972585c5112e25d to your computer and use it in GitHub Desktop.
Save pranavgarg/aa72787152c436275972585c5112e25d to your computer and use it in GitHub Desktop.
flatten a list
def flatten(l):
for el in l:
if isinstance(el, collections.Iterable) and not isinstance(el, basestring):
for sub in flatten(el):
yield sub
else:
yield el
assert [i for i in flatten([1,2,[3,4]])] == [1,2,3,4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment