0

How do you insert a new item into a numpy array in constant time.

A python list has an append method which does this, what is the equivalent in numpy. It looks like numpy.append returns a copy of the array and takes linear time.

2
  • 1
    If you aren't worried about memory, you could create something like a = numpy.zeros(10000), fill this with the numbers you want as you go along, and when you are done "appending" your N numbers to this array, do a = a[:N]. You will then only be making one copy instead of N copies. Of course, choose the size of the initial empty array to be larger than what you anticipate N to be. Commented May 24, 2013 at 14:30
  • numpy uses fixed (and exact) size array. appending to such an array is always o(n) Commented May 24, 2013 at 15:26

1 Answer 1

1

The commenters on the question are right: numpy arrays are different from Python lists and so the numpy append method is often not a good choice. This can be particularly tricky when trying to append to a numpy array quickly. The append method for a numpy array returns a copy of the array with new items added to the end. This answer has a great list of suggestions of Numpy methods to use for this, and rightly mentions that the best way to do this is to initially allocate the array with its final size. For cases where (1) I don't know what the final array size should be and (2) I need better performance, I often use,

a.resize(np.size(a) + 1, refcheck=False)
a[-1] = foo

where a is a numpy array. Watch out! a.resize is not the same as np.resize(a, ...). If the array can be resized without moving it, this operation is quick. Because the array might be moved by resize, references to the array (e.g., doing b = a before the resize) are not safe after resizing. I normally set refcheck=False but I then make sure not to use any arrays that might have referenced a before resizing.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.