I have an array of lists and I want to convert it into single dimensional array. I can do it with 2 "for" loops but one prohibition is that I have to do it as it was N-dimensional array.
original_list = [[2,4,3,[1,2]],[1,5,6], [9], [7,9,0]]
I have an array of lists and I want to convert it into single dimensional array. I can do it with 2 "for" loops but one prohibition is that I have to do it as it was N-dimensional array.
original_list = [[2,4,3,[1,2]],[1,5,6], [9], [7,9,0]]
Just use chain
from itertools
:
Code:
import itertools as it
original_list = [[2,4,3,[1,2]],[1,5,6], [9], [7,9,0]]
print(original_list)
new_list = list(it.chain.from_iterable(original_list))
print(new_list)
Output:
[[2, 4, 3, [1, 2]], [1, 5, 6], [9], [7, 9, 0]]
[2, 4, 3, [1, 2], 1, 5, 6, 9, 7, 9, 0]