30

Lists have a very simple method to insert elements:

a = [1,2,3,4]
a.insert(2,66)
print a
[1, 2, 66, 3, 4]

For a numpy array I could do:

a = np.asarray([1,2,3,4])
a_l = a.tolist()
a_l.insert(2,66)
a = np.asarray(a_l)
print a
[1 2 66 3 4]

but this is very convoluted.

Is there an insert equivalent for numpy arrays?

3 Answers 3

40

You can use numpy.insert, though unlike list.insert it returns a new array because arrays in NumPy have fixed size.

>>> import numpy as np
>>> a = np.asarray([1,2,3,4])
>>> np.insert(a, 2, 66)
array([ 1,  2, 66,  3,  4])
Sign up to request clarification or add additional context in comments.

2 Comments

That is very intuitive, I don't know why I didn't think of trying it. Thank you very much @Ashwini!
More about numpy.insert is here medium.com/@panjeh/…
22

If you just want to insert items in consequent indices, as a more optimized way you can use np.concatenate() to concatenate slices of the array with your intended items:

For example in this case you can do:

In [21]: np.concatenate((a[:2], [66], a[2:]))
Out[21]: array([ 1,  2, 66,  3,  4])

Benchmark (5 time faster than insert ):

In [19]: %timeit np.concatenate((a[:2], [66], a[2:]))
1000000 loops, best of 3: 1.43 us per loop

In [20]: %timeit np.insert(a, 2, 66)
100000 loops, best of 3: 6.86 us per loop

And here is a benchmark with larger arrays (still 5 time faster):

In [22]: a = np.arange(1000)

In [23]: %timeit np.concatenate((a[:300], [66], a[300:]))
1000000 loops, best of 3: 1.73 us per loop                                              

In [24]: %timeit np.insert(a, 300, 66)
100000 loops, best of 3: 7.72 us per loop

6 Comments

Never would've thought that using np.concatenate() was faster. Thank you Kasramvd!
@Gabriel You're welcome. That's actually because that np.concatenate need really fewer operations for doing this task rather than insert which will find the index first do a lot of checks and creates the new ones.
concatenate is compiled code, insert is a relatively complicated Python function (which you can study with np.source(np.insert)).
@hpaulj You mean C is only 5 time faster than python? At first I thought that this might be the reason but the difference in run times seems just like a difference in functionality.
After trying to replicate insert for the simplest insertion case, I think most of the extra time is due to its greater generality. The core task of copying several slices of a to the new array takes about the same time as with concatenate. But it spends more time setting things up and manipulating the slicing tuple.
|
-1

To add elements to a numpy array you can use the method 'append' passing it the array and the element that you want to add. For example:

import numpy as np dummy = [] dummy = np.append(dummy,12)

this will create an empty array and add the number '12' to it

1 Comment

I don't think this answers the OP question. Since the OP wanted to insert at an arbitrary position and not only at the end.

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.