How can I append a numpy array of N-Length to another array of N-dimensions?
<p><strong>Situation</strong></p>

<p>I assumed this would be easy - but turns out there are a few restrictions. 
I have an empty array, that at this point, is empty and has unknown dimensions.</p>

<pre><code>mainArray = np.array([])
</code></pre>

<p>later on, I want to append arrays to my main array, which are of different lengths.</p>

<p><strong>I have tried</strong></p>

<p>*Please assume all arrays I have attempted to append are the result of <code>np.zeros(n)</code></p>

<p>I have tried <code>np.append()</code> but this does not maintain the correct dimensions (assumes I want a linear array).</p>

<p>I have tried <code>np.concatenate()</code> however, this error </p>

<pre><code>TypeError: only length-1 arrays can be converted to Python scalars
</code></pre>

<p>implies I cannot concatenate to an empty array...?</p>

<p>I have tried <code>np.vstack()</code> but get</p>

<pre><code>ValueError: all the input array dimensions except for the concatenation axis must match exactly
</code></pre>

<p>...which implies I cannot have added arrays of different lengths?</p>

<p><strong>Question</strong></p>

<p>How can I append n-length arrays to an empty n-dimensional array?</p>

<p><strong>update</strong></p>

<p>Here is an example of an output:</p>

<pre><code>[[0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0]]
</code></pre>

<p>Where length of 3 is a variable</p>

<p>Your starting array is not empty (ok, it does have 0 elements), and not of unknown dimensions.  It has a well defined shape and number of dimensions (1d).</p>

<pre><code>In [704]: a=np.array([])
In [705]: a.shape
Out[705]: (0,)
In [706]: a.ndim
Out[706]: 1
</code></pre>

<p>Some examples on concatenate that work with <code>a</code></p>

<pre><code>In [708]: np.concatenate((a,a,a,a)).shape
Out[708]: (0,)
In [709]: np.concatenate((a,np.zeros(3))).shape
Out[709]: (3,)
</code></pre>

<p>As a general rule, don't start with an 'empty' array and try to append to it repeatedly.  That's a <code>list</code> approach, and is not efficient with arrays.  And because of the dimensionality issue, might not work.</p>

<p>A correct way of doing a repeated append is something like:</p>

<pre><code>alist = []
for i in range(3):
     alist.append([1,2,3])
np.array(alist)
</code></pre>

<p>Are the sublists all the same length or not?  In your last example, they differ, and the array version is  <code>dtype=object</code>.  It is 1d, with pointers to lists else where in memory - i.e. glorified list.</p>

<pre><code>In [710]: np.array([[0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0]])
Out[710]: array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0]], dtype=object)
</code></pre>

<p>This is a very different thing from what you'd get by <code>vstack</code> of 3 arrays of the same length.</p>

<p>I think you need more practice with basic array construction, and the meaning of <code>shape</code> and dimensions.  The title itself shows some confusion - <code>array of length N</code> <code>array of N-dimensions</code>. Those are very different descriptions.</p>

<p>============</p>

<p>The basic point with <code>concatenate</code> is that a <code>(n1,m)</code> array can be joined with a <code>(n2,m)</code> array to produce a <code>(n1+n2,m)</code> array.  Similarly for other dimensions.  If one array is 1d <code>(m,)</code> it needs to be expanded to <code>(1,m)</code> to perform this concatenation. <code>vstack</code> does this kind of expansion for you.</p>

<p>This means that a <code>(0,)</code> shape array can be concatenated horizontally with other 1d arrays, but can't participate in a 2d vertical concatenation except with a <code>(n2,0)</code> array.  A dimension may have size 0, but this is rarely useful, since it doesn't contain any  data.</p>

python arrays numpy