Initialise numpy array of unknown length

arrays, numpy, python

Solution

Build a Python list and convert that to a Numpy array. That takes amortized O(1) time per append + O(n) for the conversion to array, for a total of O(n).

    a = []
    for x in y:
        a.append(x)
    a = np.array(a)

Problem

I want to be able to 'build' a numpy array on the fly, I do not know the size of this array in advance. For example I want to do something like this: ``` a= np.array() for x in y: a.append(x) ``` Which would result in a containing all the elements of x, obviously this is a trivial answer. I am just curious whether this is possible?

Original source