Created
August 17, 2018 18:37
-
-
Save ccalabro/57fe6af465616aa1c617a16ebeab5804 to your computer and use it in GitHub Desktop.
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_lists(integer_list): | |
list_to_return = [] | |
for element in integer_list: | |
""" | |
if element is a list, recursivelly call flatten_lists and extend the list | |
else, append the element to the list to return | |
""" | |
if type(element) == list: | |
list_to_return.extend(flatten_lists(element)) | |
else: | |
list_to_return.append(element) | |
return list_to_return | |
print('[[1, 2, [3]], 4] -> {}'.format(flatten_lists([[1, 2, [3]], 4]))) | |
print('[1, 2, [3, 4], [5, 6], [7], [8, 9, [10]]] -> {}'.format( | |
flatten_lists([1, 2, [3, 4], [5, 6], [7], [8, 9, [10]]]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment