Last active
September 14, 2017 15:32
-
-
Save smdabdoub/7201411929a4e94d9aff34752ad6d089 to your computer and use it in GitHub Desktop.
flatten a 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
# 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