How to flatten a nested 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
l = [ [1, 2], [3, 4, 5] ] | |
# Option 1 | |
# Nice because it works not only with lists but all kinds of iterators | |
flattened1 = list(itertools.chain(*l)) | |
# Option 2 | |
flattened2 = sum(l, []) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment