Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
using flatten() from pandas.core.common solution in 3rd party category for Flatten a List by pacurar.sebastian90
from pandas.core.common import flatten
def flat_list(array: list) -> list:
"""
What flatten does, is to use recursion over the list until the list is completely flat.
Since pandas is built on top of numpy, it means it's a fast solution
"""
return list(flatten(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')
March 29, 2021
Comments: