1

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?

2 Answers 2

3

How about this:

c = np.hstack([b, a])
Sign up to request clarification or add additional context in comments.

Comments

2

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.])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.