pythonarrayslistnumpy

Python list with NumPy arrays as elements


I have a list with NumPy arrays as elements that looks like this:

[array([ 0.2, -2.3,  5.3]),
 array([-1.6, -1.7,  0.3]),
 array([ 2.4, -0.2, -3.0]),
 array([-4.1, -2.3, -2.7])]

and I want to convert it into 3 lists, each with elements from the columns of the above list. So the desired outcome looks like

list1 = [0.2, -1.6, 2.4, -4.1]
list2 = [-2.3, -1.7, -0.2, -2.3]
list3 = [5.3, 0.3, -3.0, -2.7]

Solution

  • You can achieve this by using NumPy's transpose or T method like this:

    transposed = np.array(array_list).T
    list1, list2, list3 = transposed.tolist()
    

    Full example