There wasn't much hint on performance for the different ways so I performed a test on removing 5000 items from 50000 in all 3 generally different approaches, and for me numpy was the winner (if you have elements that fit in numpy):
- 7.5 sec for the enumerated list comprehension [4.5 sec on another PC]
- 0.08 sec for deleting items in reverse order [0.017 (!) sec]
- 0.009 sec for numpy.delete [0.006 sec]
Here's the code I timed (in the third function conversion from/to list may be removed if working directly on numpy arrays is ok):
import time
import numpy as np
import random
def del_list_indexes(l, id_to_del):
somelist = [i for j, i in enumerate(l) if j not in id_to_del]
return somelist
def del_list_inplace(l, id_to_del):
for i in sorted(id_to_del, reverse=True):
del(l[i])
def del_list_numpy(l, id_to_del):
arr = np.array(l, dtype='int32')
return list(np.delete(arr, id_to_del))
l = range(50000)
random.shuffle(l)
remove_id = random.sample(range(len(l)), 5000) # 10% ==> 5000
# ...