[Python] Speed up this code?

Martin Kelly aomighty at gmail.com
Thu May 25 19:34:16 PDT 2006


Thanks all, it worked! Quite zippy now :).

What is the purpose of sets? I didn't know about them yet.

It appears Tim Chase responded to my query on comp.lang.python as well ;).

Martin

Bob Miller wrote:
> 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]
> 


More information about the Python mailing list