Skip to content

Instantly share code, notes, and snippets.

@haliphax
Last active February 28, 2024 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haliphax/faaceca42d09106ea7af38412a2012bb to your computer and use it in GitHub Desktop.
Save haliphax/faaceca42d09106ea7af38412a2012bb to your computer and use it in GitHub Desktop.
Flatten irregular array
def flatten(source):
"""
Flattens an irregular array into a single dimension.
:param list source: The array to flatten
:rtype: list
:returns: The flattened array
"""
assert isinstance(source, (list, tuple)), 'Only lists and tuples are supported'
dest = list()
for el in source:
if isinstance(el, (list, tuple)):
dest += flatten(el)
else:
dest.append(el)
return dest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment