Skip to content

Instantly share code, notes, and snippets.

@zerodayyy
Created November 2, 2020 13:04
Show Gist options
  • Save zerodayyy/fa0287e84c4c1a4653f99561c716af92 to your computer and use it in GitHub Desktop.
Save zerodayyy/fa0287e84c4c1a4653f99561c716af92 to your computer and use it in GitHub Desktop.
[Python] Flatten a list with arbitrary dimensions
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