1

Very basic question. Numpy is faster than list and does more than list.

I can create a list like

A = [1,2,3,4]

And I can also do similar things with NumPy.

B = np.array([1,2,3,4])

Is there any specific reason to use list if NumPy can do everything that list can do?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Dong-gyun Kim
  • 411
  • 5
  • 23
  • 1
    https://stackoverflow.com/questions/993984/what-are-the-advantages-of-numpy-over-regular-python-lists – Berkay Öztürk Jul 13 '20 at 14:49
  • Note that you used a list when creating `B`. `B=np.array(A)` is equivalent. And that `np.array(...)` call is not a trivial one; it takes time; more when `A` is large. – hpaulj Jul 13 '20 at 15:39

2 Answers2

2

Lists are typically more capable when it comes to appending and removing items. Lists are also not typed, whereas numpy arrays are. You can add any object to a list, but can only add values of a certain type to a numpy array.

Numpy arrays are optimized for numerical matrix calculations. They serve different purposes. If you wish to simply do mathematical operations on vector-like objects, numpy is the way to go. If you need to store information of varied types and amounts, lists will be better and in many cases be more efficient.

M Z
  • 4,571
  • 2
  • 13
  • 27
1

If you want to put various data types in a data container then you should use list. Array can store one type of data in it. And arrays are generally suitable for arithmetic operations. Though you can use list for arithmetic computation, arrays are much programmer-friendly for those tasks. For example:

If you have a list of numbers and you wanna do some basic ,maths on it like this:

x=[2,4,6,8,10]
print(x/2)

This will throw an error saying something like 'unsupported operand for list'

Doing this with arrays will successfully spit out the expected output.

ajay sagar
  • 169
  • 1
  • 6