Skip to content

Instantly share code, notes, and snippets.

@forrestchang
Created February 22, 2017 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forrestchang/dd8ba131a3fcf8736d6897173eacdf51 to your computer and use it in GitHub Desktop.
Save forrestchang/dd8ba131a3fcf8736d6897173eacdf51 to your computer and use it in GitHub Desktop.
Flatten (an irregular) list of lists in Python
# 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