I have a somewhat peculiar structure of python list of lists that I need to convert to a numpy array, so far I have managed to simply get by using np.array(myarray, dtype = object), however a seemingly insignificant change to the structure of myarray has caused me to get an error.
I have managed to reduce my issue down into two lines of code, the following is what I was using previously and works exactly how I want it to:
import numpy as np
myarray = [np.array([[1,2,3,4],[5,6,7,8]]), np.array([[9,10],[11,12]]), np.array([[13,14],[15,16],[17,18]])]
np.array(myarray,dtype = object)
However, simply removing the last [17,18] array we have
import numpy as np
myarray = [np.array([[1,2,3,4],[5,6,7,8]]), np.array([[9,10],[11,12]]), np.array([[13,14],[15,16]])]
np.array(myarray,dtype = object)
Which gives “ValueError: could not broadcast input array from shape (2,4) into shape (2,)” when it attempts to run the second line.
It seems to me that this only happens when the arrays all have the same length but the underlying lists have different lengths, what I don’t understand is why setting dtype = object doesnt cover this especially considering it handles the more complicated list of lists shape.
Arran is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.