Skip to content

Instantly share code, notes, and snippets.

@ccalabro
Created August 17, 2018 18:37
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 ccalabro/57fe6af465616aa1c617a16ebeab5804 to your computer and use it in GitHub Desktop.
Save ccalabro/57fe6af465616aa1c617a16ebeab5804 to your computer and use it in GitHub Desktop.
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