how to remove 50000 elements from a 100000 list?

Sion Arrowsmith siona at chiark.greenend.org.uk
Fri May 5 10:47:57 EDT 2006


Tim Chase  <python.list at tim.thechases.com> wrote:
>Another attempt might be to try
>
> >>> a = [x for x in a if x not in b]
>
>However, this is still doing A*B checks, and will likely 
>degrade with as their sizes increase.

Combine this with the use of sets others have suggested if the
order of a matters, ie:

>>> bset = set(b)
>>> a = [ x for x in a if x not in bset ]

which gets you down to O(A) since set membership is O(1).

-- 
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |    -- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump



More information about the Python-list mailing list