Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a NumPy array and a python list? [duplicate]

Why do we use numpy arrays in place of lists in python? What is the main difference between them?

like image 340
Mobi Zaman Avatar asked Mar 06 '16 10:03

Mobi Zaman


People also ask

What is the difference between NumPy array and list in Python?

While Python lists store a collection of ordered, alterable data objects, NumPy arrays only store a single type of object. So, we can say that NumPy arrays live under the lists' umbrella. Therefore, there is nothing NumPy arrays do lists do not.

Is Python list faster than NumPy array?

NumPy Arrays are faster than Python Lists because of the following reasons: An array is a collection of homogeneous data-types that are stored in contiguous memory locations. On the other hand, a list in Python is a collection of heterogeneous data types stored in non-contiguous memory locations.

What is the difference between NumPy array and series?

Series as generalized NumPy array The essential difference is the presence of the index: while the Numpy Array has an implicitly defined integer index used to access the values, the Pandas Series has an explicitly defined index associated with the values.

What is NumPy array in Python?

A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension.


1 Answers

Numpy arrays is a typed array, the array in memory stores a homogenous, densely packed numbers.

Python list is a heterogeneous list, the list in memory stores references to objects rather than the number themselves.

This means that Python list requires dereferencing a pointer every time the code needs to access the number. While numpy array can be processed directly by numpy vector operations, which makes these vector operations much faster than anything you can code with list.

The drawback of numpy array is that if you need to access single items in the array, numpy will need to box/unbox the number into a python numeric object, which can make it slow in certain situations; and that it can't hold heterogeneous data.

like image 82
Lie Ryan Avatar answered Sep 19 '22 16:09

Lie Ryan