Removing multiple elements from a list in Python

Let’s say you need to filter a Python list and remove all elements that match a given criteria. If you wanted to remove a single element, you could just use ‘del list[i]’ (example). But if you wanted to remove multiple elements this might be a problem since you would be modifying and iterating over the list at the same time (keeping track of list indexes can become very confusing, very fast).

A simple solution to this problem would be to keep record of all list elements (indexes) that need to be removed, and to remove tham afterwards. Also, one other thing to be wary about is that the removal process should be done in reverse because otherwise you would shift all the elements to the ‘left’ every time you removed an element from a list and you would (again) have to keep track of list indexes.

A simple function example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
matchingFilter = ['criteria1', 'criteria2']

def criteriaFiltering(aList, matchingFilter):
   deletionIndexes = []
   i = 0

   for listLine in aList:
      for match in matchingFilter:
         if match in str(listLine):
            continue             
         else:
            deletionIndexes.append(i)
            break
      i += 1

   for number in reversed(deletionIndexes):
      del listLines[number]

   return aList

This might not be the fastest ‘algorithm’ to do the job, but it works well enough for me..