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.

abc def foo bar
  • 2,360
  • 6
  • 30
  • 41
  • 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. – SethMMorton May 24 '13 at 14:30
  • numpy uses fixed (and exact) size array. appending to such an array is always o(n) – njzk2 May 24 '13 at 15:26

1 Answers1

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.

Community
  • 1
  • 1
Alex Szatmary
  • 3,431
  • 3
  • 21
  • 30