[Python] Speed up this code?

Mike Casaday mike at mcasaday.com
Thu May 25 18:15:43 PDT 2006


Not sure if it's faster, but try this:

def rmlist(original, deletions):
   return filter(lambda i: i not in deletions, original)

On May 25, 2006, at 6:10 PM, Martin Kelly wrote:

> I'm creating a program to calculate all primes numbers in a range  
> of 0 to n, where n is whatever the user wants it to be. I've worked  
> out the algorithm and it works perfectly and is pretty fast, but  
> the one thing seriously slowing down the program is the following  
> code:
>
> def rmlist(original, deletions):
>    return [i for i in original if i not in deletions]
>
> original will be a list of odd numbers and deletions will be  
> numbers that are not prime, thus this code will return all items in  
> original that are not in deletions. For n > 100,000 or so, the  
> program takes a very long time to run, whereas it's fine for  
> numbers up to 10,000.
>
> Does anybody know a faster way to do this? (finding the difference  
> all items in list a that are not in list b)?
>
> Thanks,
> Martin
> _______________________________________________
> Python mailing list
> Python at euglug.org
> http://www.euglug.org/mailman/listinfo/python



More information about the Python mailing list