Most efficient solution?

Terry Reedy tjreedy at
Fri Jul 20 19:26:28 EDT 2001

> > def dict(a,c):
> > start = time.clock()
> > for item in a:
> > d = filter(c.has_key, a)
> this is an error.  You are creating a list containing items in 'a'
> 'c' for each item in a.  You only need to make the list once.
> with the correction dict runs in .04 seconds while comp runs in
> I would say that you still need to use dictionaries (note that this
> doesn't actually solve the problem, which is to get items in 'a' but
> not in 'b', but the following
> def dict2(a, c):
>    start = time.time()
>    d = [ i for i in a if not c.has_key(i)]
>    end = time.time()
>    print `end-start`
> does in in .14 seconds (still 400x faster than comp)

To compare program running times, they should compute the same thing.

How long does dict2 take if you remove the not, so it makes the list
of 1500 elements that revised dict does, instead of the 'not' list of

How long does dict take if you feed filter with 'lambda x,
f=c.has_key: f(x)', so it make the 'not' list of 6000 elements?

Terry J. Reedy

More information about the Python-list mailing list