This is something I didn't find so easily on the internet. It's easy to create np.arrays
, but how do you create an empty one so you can append on the go?
Asked
Active
Viewed 2.2k times
-2

Philippe Fanaro
- 6,148
- 6
- 38
- 76
-
7You shouldn't do this. Numpy arrays aren't designed to change size, build the list first and then convert to an array, or initialise an empty array of correct shape and fill values by index (if the final shape is known) – roganjosh Sep 24 '18 at 18:15
-
But if you _did_ want to do this anyway, "numpy append" in a Google search gives [this](https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.append.html) as the first result – roganjosh Sep 24 '18 at 18:17
-
You generally *don't do this* and `numpy.ndarray` objects aren't designed to allow you to do this. Numpy arrays and `list` objects have very different performance trade-offs. If you really want to do this, use a list and convert to an array at the end. – juanpa.arrivillaga Sep 24 '18 at 18:18
-
However you can stack `np.arrays` with `np.hstack` or `np.vstack`. – Josef Korbel Sep 24 '18 at 18:19
-
I've just found out that using `np.empty((0,0))` creates an empty array and then you can use `np.append()` to add items, much like lists. Do you think that's not advisable still? – Philippe Fanaro Sep 24 '18 at 18:22
-
No, it is not advisable at all. – user3483203 Sep 24 '18 at 18:22
-
Yes, because every call to append has to copy over the entire array into a new block of memory. Just because you discovered that you could initialise an empty array does not change the advice we have been giving you. – roganjosh Sep 24 '18 at 18:23
-
1@PhilippeFanaro absolutely inadvisable. `np.append` is a linear-time operation. Using it to fill a `numpy.array` will create quadratic-time behavior. In contrast, `list.append` is (amoritized) constant time, so overall, it will be linear time to construct the list. – juanpa.arrivillaga Sep 24 '18 at 18:25
-
I was just trying to figure out a hack for something that was kind of cumbersome to me, since it's sometimes tiring to go from list to arrays every time the dimensions change. And, in my opinion, for most day to day uses, it seems quite ok to sacrifice a bit of memory just for the ease of programming. – Philippe Fanaro Sep 24 '18 at 18:29
-
It's not memory, it's runtime too – roganjosh Sep 24 '18 at 18:31
-
2You're not just sacrificing memory, you're taking a *huge* hit in terms of time complexity. Beyond that, if you're trying to use numpy on lists that change size frequently, it may be time to examine why you're using `numpy`. – user3483203 Sep 24 '18 at 18:31
-
@PhilippeFanaro no, it's a rather enormous hit to time-complexity and runtime performance. O(N) vs O(N^2) – juanpa.arrivillaga Sep 24 '18 at 18:35
-
Can someone link me to Numpy docs where they say "Numpy arrays aren't designed to change size"? Thanks, I just want an official confirmation of its intention. – NoName Apr 21 '20 at 23:21
2 Answers
2
1. A Hack
You can create an empty array using the np.empty()
function and specify the dimensions to be (0, 0)
and then use np.append()
to append items later.
>>> a = np.empty((0, 0))
>>> a
array([], shape=(0, 0), dtype=float64)
>>> b = np.append(a, [1, 2])
>>> b
array([1., 2.])
2. However...
The hack above is not advisable, use it with caution. Appending to list
s has O(N)
complexity, while appending to arrays has O(N^2)
(besides different memory use). The proper way should be, then, to append to list
s. Note that using list()
on numpy arrays
to transform them into list
s is not correct, as you will get a list
of numpy arrays
. Instead, use the .tolist()
method.
>>> a = np.array([[1, 2], [3, 4]])
>>>
>>> list(a)
[array([1, 2]), array([3, 4])]
>>>
>>> a.tolist()
[[1, 2], [3, 4]]

Philippe Fanaro
- 6,148
- 6
- 38
- 76
-1
It is advised to use normal python lists and then at the end convert the list to a numpy array.
x = np.asarray(x)

Atul Shanbhag
- 636
- 5
- 13