2

How can I place a value inside an array witha specified index. Like how can I place the number 3 inside between the 4th and the 5th element in array.

number = 3
index= 5
array= np.array([ 31, 28, 31, 30, 31, 30, 31, 31])

Expected Output

[ 31, 28, 31, 30, 3, 31, 30, 31, 31]

2 Answers 2

4

Use np.insert

import numpy as np

np.insert(array, index, number)
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the numpy.insert function to insert a value at a specified point in a numpy.array. Here is how you would use it in your case:

array = np.array([ 31, 28, 31, 30, 31, 30, 31, 31])
np.insert(array, 5, 3)

The second argument is the index before which you wish to insert the value, which is the third argument. See the documentation here for more information, especially for higher-dimensional arrays, which can get a bit more complicated.

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.