Created
November 2, 2020 13:04
-
-
Save zerodayyy/fa0287e84c4c1a4653f99561c716af92 to your computer and use it in GitHub Desktop.
[Python] Flatten a list with arbitrary dimensions
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
def flatten(l, result=[]): | |
"""Flatten a list with arbitrary dimensions. | |
Args: | |
l (List[Any]): The list to flatten. | |
Returns: | |
List[Any]: Flattened list. | |
""" | |
for item in l: | |
if isinstance(item, Iterable) and not isinstance(item, str): | |
reduce(item, result) | |
else: | |
result.append(item) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment