Suppose I have ;

```
LIST = [[array([1, 2, 3, 4, 5]), array([1, 2, 3, 4, 5],[1,2,3,4,5])] # inner lists are numpy arrays
```

I try to convert;

```
array([[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5])
```

I am solving it by iteration on vstack right now but it is really slow for especially large LIST

What do you suggest for the best efficient way?

## Best Answer

In general you can concatenate a whole sequence of arrays along any axis:

but you

dohave to worry about the shape and dimensionality of each array in the list (for a 2-dimensional 3x5 output, you need to ensure that they are all 2-dimensional n-by-5 arrays already). If you want to concatenate 1-dimensional arrays as the rows of a 2-dimensional output, you need to expand their dimensionality.As Jorge's answer points out, there is also the function

`stack`

, introduced in numpy 1.10:This takes the complementary approach: it creates a new view of each input array and adds an extra dimension (in this case, on the left, so each

`n`

-element 1D array becomes a 1-by-`n`

2D array) before concatenating. It will only work if all the input arrays have the same shape—even along the axis of concatenation.`vstack`

(or equivalently`row_stack`

) is often an easier-to-use solution because it will take a sequence of 1- and/or 2-dimensional arrays and expand the dimensionality automatically where necessary and only where necessary, before concatenating the whole list together. Where a new dimension is required, it is added on the left. Again, you can concatenate a whole list at once without needing to iterate:This flexible behavior is also exhibited by the syntactic shortcut

`numpy.r_[ array1, ...., arrayN ]`

(note the square brackets). This is good for concatenating a few explicitly-named arrays but is no good for your situation because this syntax will not accept a sequence of arrays, like your`LIST`

.There is also an analogous function

`column_stack`

and shortcut`c_[...]`

, for horizontal (column-wise) stacking, as well as analmost-analogous function`hstack`

—although for some reason the latter is less flexible (it is stricter about input arrays' dimensionality, and tries to concatenate 1-D arrays end-to-end instead of treating them as columns).Finally, in the specific case of vertical stacking of 1-D arrays, the following also works:

...because arrays can be constructed out of a sequence of other arrays, adding a new dimension to the beginning.