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

Tycho Andersen tycho at tycho.ws
Fri Oct 26 17:42:17 EDT 2012


On Fri, Oct 26, 2012 at 05:36:50PM -0400, Dave Angel wrote:
> On 10/26/2012 05:26 PM, Tycho Andersen wrote:
> > 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.

What trailing space before the newline? The other solutions have it,
the above does not. However, the above does mutate args, which isn't
all that great. Alas, if you want the performance of mutable
structures, you're probably going to have to mutate something. (In any
case, it's easy enough to change it back, though ugly.)

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

The same reason some people enjoy sporting events: it's fun :-)

\t



More information about the Python-list mailing list