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