, , , set , .
>>> myList = ['a','b','c','d']
>>> toRemove = set([0,2])
>>> [x for i,x in enumerate(myList) if i not in toRemove]
['b', 'd']
Checking each element in myList for each inRemove element is O (n * m) (where n is the length of myList and m is the length of toRemove). If you use set, the membership check is O (1), so the whole procedure becomes O (n). Keep in mind, however, the difference in speed will not be noticeable if toRemove is really large (say, more than a thousand).
source
share