Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
This one was a struggle solution in Clear category for Flatten a List by bk92
def flat_list(array):
while [isinstance(a,list) for a in array].count(True) > 0:
new = []
for a in array:
if type(a) == list:
for x in a:
new.append(x)
else:
new.append(a)
array = new
return (array)
if __name__ == '__main__':
assert flat_list([1, 2, 3]) == [1, 2, 3], "First"
assert flat_list([1, [2, 2, 2], 4]) == [1, 2, 2, 2, 4], "Second"
assert flat_list([[[2]], [4, [5, 6, [6], 6, 6, 6], 7]]) == [2, 4, 5, 6, 6, 6, 6, 6, 7], "Third"
assert flat_list([-1, [1, [-2], 1], -1]) == [-1, 1, -2, 1, -1], "Four"
print('Done! Check it')
Sept. 14, 2020