# This first approach work, relying on repeated calls to list methods, # but it is not as efficient if invoked on really big data sets def removeAll(data, val): while val in data: data.remove(val) # This second approach is far more efficient in practice, but also # more intricate in terms of an algorithm. We scan through the data, # consolidating all the non-matches toward the left side of the list, # and then eventually shrinking the list to only those elements. def removeAll(data, val): opening = 0 # next place we will put a non-match scan = 0 # index at which we are scanning while scan < len(data): if data[scan] != val: data[opening] = data[scan] opening += 1 scan += 1 # at this point, all the desired contents are where we want. # just need to pop the garbage at the end for extra in range(scan-opening): data.pop()