Performance problem with filtering

Delaney, Timothy tdelaney at avaya.com
Wed Mar 13 23:51:22 EST 2002


> From: Sheila King [mailto:usenet at thinkspot.net]
> 
> On Thu, 14 Mar 2002 15:06:09 +1100, "Delaney, Timothy" 
> <tdelaney at avaya.com>
> wrote in comp.lang.python in article
> <mailman.1016078842.13805.python-list at python.org>:
> 
> > Bah ...
> > 
> > results = filter(lambda k: not c.has_key(k), b)
> > 
> > That will slow it down a bit (using the lambda) but will 
> give the right
> > result ;)
> 
> How does something with list comprehensions compare? Like:
> 
> b = [entry for entry in b if entry not in a]
> 
> Is this as slow as the original method suggested? Or is it closer to
> dictionary performance?

Essentially, that is identical to the original method. Simply expand it out
... (need to use a temporary name though).

temp_b = []

for entry in b:
    if entry not in a:
        temp_b.append(entry)

b = temp_b
del temp_b

Tim Delaney




More information about the Python-list mailing list