numpy-
arr = np.array([[1, 2, 3, 4]])
row = np.array([1, 2, 3, 4])
%timeit arr[0] = row
466 ns ± 12.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
python list -
arr = [[1, 2, 3, 4]]
row = [1, 2, 3, 4]
%timeit arr[0] = row
59.3 ns ± 2.94 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each
Shouldn't numpy be the faster version here?
Here's what I'm aiming to get done -
arr = np.empty((150, 4))
while True:
row = get_row_from_api()
arr[-1] = row
numpy.arrayexample, it is assigning 4 items. To get a fair comparison, you'd need something likefor i, x in enumerate(row): arr[0][i] = xnumpy.ndarrayobject withdtype=object, but then you are essentially working with a bad list. So at that point, just use alistinstead of anumpy.ndarraylistand anumpy.ndarray. And it isn't immediately obvious exactly what you are doing with your numpy array.arr[-1] = rowrepeatedly assigns a value to the same last row ofarr. It does not 'step-through' the rows,