better way for ' '.join(args) + '\n'?

Dave Angel d at davea.name
Fri Oct 26 17:36:50 EDT 2012


On 10/26/2012 05:26 PM, Tycho Andersen wrote:
> On Fri, Oct 26, 2012 at 09:49:50AM +0200, Ulrich Eckhardt wrote:
>> Hi!
>>
>> General advise when assembling strings is to not concatenate them
>> repeatedly but instead use string's join() function, because it
>> avoids repeated reallocations and is at least as expressive as any
>> alternative.
>>
>> What I have now is a case where I'm assembling lines of text for
>> driving a program with a commandline interface. In this scenario,
>> I'm currently doing this:
>>
>>   args = ['foo', 'bar', 'baz']
>>   line = ' '.join(args) + '\n'
> Assuming it's the length of the list that's the problem, not the
> length of the strings in the list...
>
> args = ['foo', 'bar', 'baz']
> args[-1] = args[-1] + '\n'
> line = ' '.join(args)
>
> \t

Main problem with that is the trailing space before the newline.  If
that's not a problem, then fine.

Not sure why we try so hard to optimize something that's going to take
negligible time.

-- 

DaveA




More information about the Python-list mailing list