Python Forum

Full Version: NumPy and List
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is NumPy is better than Python list?
Depends on the use case, use the right tool for the right job.
Hello,
Answering to your question-
NumPy is a Python package, which has made its place in the world of scientific computing. It can deal with large data sizes, and also has a powerful N-dimensional array object along with a set of advanced functions.

Yes, a NumPy array is better than a Python list. This is in the following ways:
  • It is more compact.
  • It is more convenient.
  • It Is more efficiently.
  • It is easier to read and write items with NumPy.
(May-06-2019, 08:44 AM)dukoolsharma Wrote: [ -> ]Yes, a NumPy array is better than a Python list.
As per Yoriz, it certainly depends. Rarely is one thing entirely better than another in programming. Any additional complexity should be justified; if I saw in code review someone importing numpy when a regular Python list would be fine, I would call them out on it.
Sometimes numpy works slower than native python lists, e.g.
when appending the data to existing arrays
import numpy as np
x = np.array(list())
y = list()
%timeit -n 100000 np.append(x, '2') 
Output:
10.5 µs ± 1.1 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit -n 100000 y.append('2')
Output:
108 ns ± 4.11 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
The same behavior is true when working with big arrays.
Numpy is a good choice when you need to perform element-wise operations over arrays. Having a lot of
functions and nd-array-arithmetic operations implemented in C, numpy allows to avoid slow python loops when working with arrays (in general, it invokes loops implemented in C under the hood).