Created
April 12, 2019 05:03
-
-
Save Beefster09/7be682b8b28d90d5bbb5310efbf2f5e4 to your computer and use it in GitHub Desktop.
Flatten an arbitrarily nested array with Python
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
import itertools | |
def flatten(seq): | |
if isinstance(seq, list): | |
return list(itertools.chain.from_iterable(map(flatten, seq))) | |
else: | |
return [seq] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment