[Numpy-discussion] Stacking arrays along new dimension in front

Skipper Seabold jsseabold@gmail....
Tue Jul 20 09:24:56 CDT 2010


On Tue, Jul 20, 2010 at 5:11 AM, Gael Varoquaux
<gael.varoquaux@normalesup.org> wrote:
> Is there in numpy a function that does:
>
>    np.concatenate([a_[np.newaxis] for a_ in a])
>
> ?
>
> ie: add a dimension in front and stack along this dimension, just like
>
>    np.array(a)
>
> would do, but more efficient.
>
> This is something that do all the time. Am I the only one?
>

Will one of the stack functions do?  I take it your a looks something like

a = [np.arange(1000), np.arange(1000), np.arange(1000)]

np.all(np.vstack(a) == np.concatenate([a_[None] for a_ in a]))
# True

It's about the same speed-wise as concatenate, but it's more terse and
faster than np.array if you already have a list of arrays.

Skipper


More information about the NumPy-Discussion mailing list