I am trying to write a function called flatten_list that takes as input a list which may be nested, and returns a non-nested list with all the elements of the input list.
My code:
def flatten_list(alist):
"""
>>> flatten_list([1,2,3])
[1, 2, 3]
>>> flatten_list([1, [2,3], [4, 5], 6])
[1, 2, 3, 4, 5, 6]
"""
flat_list = []
for element in alist:
flat_list += element
return flat_list
This code works for lists with strings, but not integer values. How can I change the code so that it works for both?
thanks