how can this iterator be optimized?

Basilisk96 basilisk96 at gmail.com
Wed Feb 11 23:26:12 EST 2009


> Don't use a generator then. If you're going to finally return a list (and  
> sorted does exactly that), build a list right from the start:

Good point. However, for the sake of argument, what if I removed the
sorting requirement and simply wanted the generator? I usually strive
for comprehensions if a for loop can be reduced to such.  Would there
be any speed advantage in this case of a comprehension-style generator
vs. a for-yield loop (assuming there is a way to call func(s) once per
iteration in the comprehension)?  In my example, func() operates on
filename strings, so it's not too bad.. but it's possible for this
pattern to apply to more substantial operations.  My conjecture is
that higher func() loads would favor more the use of a simple for-
yield loop.

Cheers,
Basilisk96



More information about the Python-list mailing list