[Tutor] Loop optimization

Kent Johnson kent37 at tds.net
Mon Aug 20 15:58:14 CEST 2007


wormwood_3 wrote:
> Hello tutors,
> 
> I am trying to understand the best cases in which to use for loops,
list comprehensions, generators, and iterators. I have a rather simple
process that I made initially as a for loop:
> 
>         self.potdomains = []
>         for word in self.dictcontents:
>             self.potdomains.append(word + suffix1)
>             self.potdomains.append(word + suffix2)
> 


> What I am wondering is, would it be better in any way to do it using
> a  for loop like this, or instead to use a list comprehension (which would
> just add readability, not change the logic, I believe),

I think what you have is pretty clear. I can't think of a way to do this 
with a single list comprehension because you add two items to the list 
each time through the loop. You could use a list comp and a generator 
expression, but the order of entries in the result will be different:
self.potdomains = [ word + suffix1 for word in self.dictcontents ]
self.potdomains.extend(word + suffix2 for word in self.dictcontents)

> or a generator,

def suffixGen(words):
   for word in words:
     yield word + suffix1
     yield word + suffix2

self.potdomains = list(suffixGen(self.dictcontents))

> or an iterator. My current thought it that a generator would be more
> useful if I needed to have more control over the process while running,
> perhaps to add error checking or some other potential interruption. But
> in purely in terms of running speed, which option would be best?

The only way to know for sure is to try it with your data. Use the 
timeit module to test.

My guess is that an optimized version of your code will be fastest, 
something like

potdomains = []
append_ = potdomains.append
for word in self.dictcontents:
   append_(word+suffix1)
   append_(word+suffix2)
self.potdomains = potdomains

This takes all attribute lookups out of the loop. Put this in a function 
(method) and make sure suffix1 and suffix2 are local variables (probably 
function parameters). Then test :-)

Kent


More information about the Tutor mailing list