If performance is a problem and you are happy to use a third-party library, use numpy .
The Python standard library is great for many things. Computing on numeric arrays is not one of them.
import numpy as np mylist = np.array([4, 1, 2, 6, 1, 0, 9, 8, 0, 9]) mylist = np.delete(mylist, np.where(mylist == 0)[0][1:]) # array([4, 1, 2, 6, 1, 0, 9, 8, 9])
Here, the first argument to np.delete is the input array. The second argument retrieves the indices of all occurrences of 0, then retrieves the second instance forward.
Benchmark Performance
Tested on Python 3.6.2 / Numpy 1.13.1. Performance will be system and array specific.
%timeit jp(myarr.copy()) # 183 µs %timeit vui(mylist.copy()) # 393 µs %timeit original(mylist.copy()) # 1.85 s import numpy as np from collections import Counter myarr = np.array([4, 1, 2, 6, 1, 0, 9, 8, 0, 9] * 1000) mylist = [4, 1, 2, 6, 1, 0, 9, 8, 0, 9] * 1000 def jp(myarr): return np.delete(myarr, np.where(myarr == 0)[0][1:]) def vui(mylist): return [0] + list(filter(None, mylist)) def original(mylist): for i in mylist: if mylist.count(0) > 1: mylist.remove(0) return mylist