Faster way to do this?

Terry Reedy tjreedy at udel.edu
Wed May 21 12:25:35 EDT 2003


"Freddie" <oinkfreddie at oinkmadcowdisease.oinkorg> wrote in message
news:Xns93829DCB7A2C1freddiethescaryeleph at 203.10.110.105...
>temp = chars[:]
...

This snippet

> wlen = len(word)
> for i in range(wlen):
> if word[i] in temp:
> temp.remove(word[i])
...
can be replaced by by the slightly faster

for w in word:
  if w in temp:
    temp.remove(w)

since neither wlen nor i have any other use in this code than to drive
this part of the loop.

In general, if temp were a dict or, in 2.3, a set, 'in' would be much
faster, and
    del temp[w]
would be faster than .remove.  In 2.3, temp = set(chars) would replace
the copy.  Though with chars short, the speedup would not be much.

Terry J. Reedy









More information about the Python-list mailing list