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

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Oct 26 05:36:22 EDT 2012


On Fri, 26 Oct 2012 09:49:50 +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'
> 
> So, in other words, I'm avoiding all the unnecessary copying, just to
> make another copy to append the final newline.

*shrug*

The difference between ' '.join(sequence) and (' '.join(sequence) + '\n') 
is, in Big Oh analysis, insignificant. The first case does O(N) 
operations, the second does O(N) + O(N) = 2*O(N) operations, which is 
still O(N). In effect, the two differ only by an approximately constant 
factor.

If you really care, and you don't mind ending your last line with a 
space, just append '\n' to the sequence before calling join.


> The only way around this that I found involves creating an intermediate
> sequence like ['foo', ' ', 'bar', ' ', 'baz', '\n']. This can be done
> rather cleanly with a generator:
> 
>    def helper(s):
>        for i in s[:-1]:
>             yield i
>             yield ' '
>        yield s[-1]
>        yield '\n'
>    line = ''.join(tmp(args))
> 
> Efficiency-wise, this is satisfactory. 

Have you actually tested this? I would not be the least surprised if 
that's actually less efficient than the (' '.join(seq) + '\n') version.


-- 
Steven



More information about the Python-list mailing list