+1 vote

Best answer

You can use any of these two, but if you are concerned about CPU time, you should NOT use numpy array. If you have to create a small array/list by appending elements to it, both numpy array and list will take the same time. But as the number of elements increases, numpy array becomes too slow. However, you can convert a list to a numpy array and vice versa. Have a look at the following example.

import time

import numpy as npstart = time.time()

a=[]

for i in range(500000):

a.append(i)

a = np.array(a)

end = time.time()

print (end-start)

print('-----------------')

The above code took 0.10576796531677246 seconds.start = time.time()

a=np.array([])

for i in range(500000):

a = np.append(a, i)

print (len(a))

end = time.time()

print (end-start)

print('-----------------')

The above code took 97.84864020347595 seconds.

Here, I have only 500k elements. As the number of elements increases, the performance of numpy array degrades. So, python list should be your choice.