Created
October 28, 2016 18:34
-
-
Save pranavgarg/aa72787152c436275972585c5112e25d to your computer and use it in GitHub Desktop.
flatten a list
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 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