When I create an empty numpy array using foo = np.empty(1) the resulting array contains a float64:
>>> foo = np.empty(1)
>>> foo
array([ 0.])
>>> type(foo[0])
<type 'numpy.float64'>
Why doesn't it just return array([])?
You didn't read the documentation, which says:
Return a new array of given shape and type, without initializing entries.
empty has nothing to do with creating an array that is "empty" in the sense of having no elements. It just means the array doesn't have its values initialized (i.e., they are unpredictable and depend on whatever happens to be in the memory allocated for the array).
list.append requires creating a whole new array. Because of this, numpy arrays are not well suited for tasks that require arrays of dynamically changing size.
np.empty()is not the same as a list[]. It requires a parameter which you gave it. It sounds like you want a 0 size array. But why? That's rarely useful. See stackoverflow.com/questions/44872454/…np.empty()is shape. Shouldn't the code I used just generate an empty array of dimensions 1? By my understanding, if I saidnp.empty(2)I'd expect to getarray([[]]. How would I get that?[1],[1, 2], and[1, 2, 3]are all one-dimensional, but they have different shapes.emptyis probably not the best name for this function, given that anempty listhas a very different meaning.np.zerosornp.onesmight be a better starting point.