Last active
February 28, 2024 18:26
-
-
Save haliphax/faaceca42d09106ea7af38412a2012bb to your computer and use it in GitHub Desktop.
Flatten irregular array
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(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