LWZ
LWZ

Reputation: 12338

in NumPy, how to insert an array into another array

For example, if I have a NumPy array

import numpy as np

a = np.arange(10)
b = np.zeros(5)

How can I insert b to the beginning of a?

I know I can make a new array of size len(a)+len(b) and do slice assignment, but is there a way to directly insert the array?

Upvotes: 1

Views: 4016

Answers (2)

Blender
Blender

Reputation: 298096

You can use numpy.concatenate:

>>> np.concatenate((b, a))
    array([ 0.,  0.,  0.,  0.,  0.,  0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,
        8.,  9.])

Upvotes: 2

Luke
Luke

Reputation: 11644

How about this:

c = np.hstack([b, a])

Upvotes: 3

Related Questions