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?

2
  • 1
    stackoverflow.com/questions/993984/… Commented Jul 13, 2020 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. Commented Jul 13, 2020 at 15:39

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.