+3 votes
in Programming Languages by (73.2k points)
I have to generate a very large list/array by appending elements to an empty list/array. Should I use numpy array or python list?

1 Answer

+1 vote
by (346k points)
selected by
 
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 np

start = 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. 


...