Created
February 22, 2017 13:08
-
-
Save forrestchang/dd8ba131a3fcf8736d6897173eacdf51 to your computer and use it in GitHub Desktop.
Flatten (an irregular) list of lists in Python
This file contains hidden or 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
# http://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists-in-python | |
def flatten(nested_list): | |
for i in nested_list: | |
if isinstance(i, collections.Iterable) and not isinstance(i, (str, bytes)): | |
yield from flatten(i) | |
else: | |
yield i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment