[Python] Speed up this code?

Bob Miller kbob at jogger-egg.com
Thu May 25 18:38:53 PDT 2006


Martin Kelly wrote:

> def rmlist(original, deletions):
>    return [i for i in original if i not in deletions]

Is deletions a list?  If it is, turn it into a set.  Testing whether
an item is in a list takes O(n) time.  Testing whether an item is in a
set takes O(1) time.

def rmlist(original, deletions):
   delset = set(deletions)
   return [i for i in original if i not in delset]

-- 
Bob Miller                              K<bob>
                                        kbob at jogger-egg.com


More information about the Python mailing list