Skip to content

Instantly share code, notes, and snippets.

@smdabdoub
Last active September 14, 2017 15:32
Show Gist options
  • Save smdabdoub/7201411929a4e94d9aff34752ad6d089 to your computer and use it in GitHub Desktop.
Save smdabdoub/7201411929a4e94d9aff34752ad6d089 to your computer and use it in GitHub Desktop.
flatten a list of lists in python
# Original discussion
# https://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python
# vanilla python list comprehension
flatten = lambda lst: [item for sublist in lst for item in sublist]
# with itertools
flatten_chain = lambda lst: list(itertools.chain(*lst))
# with itertools >= 2.6
flatten_chain2 = lambda lst: list(itertools.chain.from_iterable(lst))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment