Using += in a loop
Thomas Jensen
spam at ob_scure.dk
Tue Jul 30 15:04:19 EDT 2002
Bill Dandreta wrote:
> Hi Alex,
>
> On Sat, 27 Jul 2002 20:42:03 GMT, Alex Martelli <aleax at aleax.it>
> wrote:
>
>>As a nice plus, it's also *WAY, WAY* faster. A loop with += takes
>>O(N squared) time, joiner.join takes O(N) time, where N is the
[snip]
> I am using the following code structure in some very long (10's of
> thousands of iterations) loops.
>
> Is there a more efficient way that doesn't use +=?
>
> Would s5 = '%s,"%s"' % (s5,x[1]) be better?
Probably not (it would still have the O(N squared) charactaristics).
Instead try:
s5_list = []
for ...:
s5_list.append(',"%s"' % x[1])
s5 = ''.join(s5_list)
--
Best Regards
Thomas Jensen
(remove underscore in email address to mail me)
More information about the Python-list
mailing list