How to extend an array in-place in Numpy? How to extend an array in-place in Numpy? arrays arrays

How to extend an array in-place in Numpy?


Imagine a numpy array as occupying one contiguous block of memory. Now imagine other objects, say other numpy arrays, which are occupying the memory just to the left and right of our numpy array. There would be no room to append to or extend our numpy array. The underlying data in a numpy array always occupies a contiguous block of memory.

So any request to append to or extend our numpy array can only be satisfied by allocating a whole new larger block of memory, copying the old data into the new block and then appending or extending.

So:

  1. It will not occur in-place.
  2. It will not be efficient.


You can use the .resize() method of ndarrays. It requires that the memory is not referred to by other arrays/variables.

import numpy as npret = np.array([])for i in range(100):    tmp = np.random.rand(np.random.randint(1, 100))    ret.resize(len(ret) + len(tmp)) # <- ret is not referred to by anything else,                                    #    so this works    ret[-len(tmp):] = tmp

The efficiency can be improved by using the usual array memory overrallocation schemes.


The usual way to handle this is something like this:

import numpy as npret = []for i in range(100000):  tmp =  get_input(i)  ret.append(np.zeros(len(tmp)))  ret.append(np.zeros(fixed_length))ret = np.concatenate(ret)

For reasons that other answers have gotten into, it is in general impossible to extend an array without copying the data.