Use numpy.delete which is definitely faster (376 times, as shown later) than python lists.
First method (using numpy):
import numpy as np
arr = np.array([0,3,5,7])
# [0,3,5,7]
indexes = [0,3]
np.delete(arr, indexes)
# [3,5]
Second method (using a python list):
arr = [0,3,5,7]
# [0,3,5,7]
indexes = [0,3]
for index in sorted(indexes, reverse=True):
del arr[index]
arr
# [3,5]
Code to benchmark the two methods on an array of 500000 elements, deleting half of the elements randomly:
import numpy as np
import random
import time
start = 0
stop = 500000
elements = np.arange(start,stop)
num_elements = len(temp)
temp = np.copy(elements)
temp2 = elements.tolist()
indexes = random.sample(range(0, num_elements), int(num_elements/2))
start_time = time.time()
temp = np.delete(temp, indexes)
end_time = time.time()
total_time = end_time - start_time
print("First method: ", total_time)
start_time = time.time()
for index in sorted(indexes, reverse=True):
del temp2[index]
end_time = time.time()
total_time = end_time - start_time
print("Second method: ", total_time)
# First method: 0.04500985145568848
# Second method: 16.94180393218994
The first method is about 376 times faster than the second one.