Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'args': 0.04; 'function,': 0.07; 'repeated': 0.07; 'trailing': 0.07; 'list...': 0.09; 'cc:addr:python-list': 0.10; 'commandline': 0.16; 'concatenate': 0.16; 'eckhardt': 0.16; 'fine.': 0.16; 'oct': 0.16; 'wrote:': 0.17; 'assuming': 0.22; 'this:': 0.23; 'cc:no real name:2**0': 0.24; 'cc:2**1': 0.24; 'least': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'lines': 0.28; "i'm": 0.29; 'fri,': 0.30; '+0200,': 0.33; 'problem': 0.33; 'text': 0.34; 'list': 0.35; 'problem,': 0.35; 'doing': 0.35; 'pm,': 0.35; 'something': 0.35; 'but': 0.36; 'why': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'instead': 0.39; 'received:192': 0.39; 'space': 0.39; 'where': 0.40; 'received:192.168': 0.40; '26,': 0.65; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'alternative.': 0.84; 'expressive': 0.84; 'received:74.208.4.194': 0.84; 'subject:better': 0.84 Date: Fri, 26 Oct 2012 17:36:50 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120912 Thunderbird/15.0.1 MIME-Version: 1.0 To: Tycho Andersen Subject: Re: better way for ' '.join(args) + '\n'? References: <20121026212602.GE25893@ccapuser-ubuntu.WICOURTS.GOV> In-Reply-To: <20121026212602.GE25893@ccapuser-ubuntu.WICOURTS.GOV> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:vf5+NN6kLwYM1e55613wjWz589XSb+BQ/VLLQ3F0xHi rwsNbTZUATVygg31ITBvz2NHfsZglkx9MHOA7kCTXUfL6FTGz+ kL6cVJXSyINNBHejN/Zlx5aLGeCXEFvpKacGRclMDoWvHdv7oP +O22Zir1dlu5TiZ+P9WoERF7xUSicbqGOW5zvg1rJfmCvLPgej DdnqudzvF688j5I4SyadcCKE/vPmpP+xPQbQ1nIyROKrEP7jKl adJgv66ME9M7J3kQIC3XdyCTH0Flhc7jLmdNt17Rn65CE8qJGk RwFhqC6Qv5fuJBmU9VP/5dp9DIwOFTsCx3hQ8JM2aSoEES4IA= = Cc: python-list@python.org, Ulrich Eckhardt X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351287451 news.xs4all.nl 6953 [2001:888:2000:d::a6]:34361 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32235 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