Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.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.046 X-Spam-Evidence: '*H*': 0.91; '*S*': 0.00; 'string': 0.09; 'newline': 0.09; 'subject:characters': 0.09; 'subject:string': 0.09; 'cc:addr :python-list': 0.11; '"\\n")': 0.16; 'ah,': 0.16; 'correctness': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'newline,': 0.16; 'subject:remove': 0.16; 'two.': 0.16; 'wrote:': 0.18; 'not,': 0.20; 'cc:addr:python.org': 0.22; 'exists': 0.24; 'cc:2**0': 0.24; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'lines': 0.31; 'probably': 0.32; 'subject:the': 0.34; 'subject:from': 0.34; 'one,': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'version': 0.36; 'joined': 0.36; 'entry': 0.36; 'should': 0.36; 'two': 0.37; 'list': 0.37; 'ends': 0.38; 'mine': 0.38; 'skip:- 10': 0.38; 'expect': 0.39; 'sure': 0.39; 'how': 0.40; 'blank': 0.60; "you'll": 0.62; 'more': 0.64; 'between': 0.67; 'results': 0.69; '2015': 0.84; 'safe.': 0.84; 'subject:long': 0.84; 'subject:very': 0.91; 'to:none': 0.92; 'differences': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=FyORLLxU7XYgSQw97wNWGFfhBnG5as1Ae+MRK7sans8=; b=sTrIUe9FcYO4RFGpl+kq8cu3lqdhzwG2cxioI/qeCDd7SzBx1bE+SB78S+0vbO5kzy ZW1WWC2iXhvjL0dwLiYQM3Zz2PLmVJhWSBrCVJyC2S9iKxNDqDBi8aLb2Ogz7UvrCfoc K+y1atQM0cC0XJk2rrkDjGSZonCPf4Qedo2JZWjKjf6Fzxa6zUQ8nM5LqyxtBwxdMu8+ 2Nh6oHcMzCxSGkseFQXUIaaBYmSdpLV6C8Q6tF5xz+/m9xi3PVj3Vd19yHsH7bat4Xtc 5gz1kDltOHB0nEII9ARepkF1uqX9lhFLWksPhwv/AUb2HV8UvFhTtwoR8OYcGKhYlYSr X5yg== MIME-Version: 1.0 X-Received: by 10.50.141.164 with SMTP id rp4mr5252793igb.2.1431795546649; Sat, 16 May 2015 09:59:06 -0700 (PDT) In-Reply-To: References: <6a383ce2-5975-4225-b4f2-f744c9d7a516@googlegroups.com> <4272d9d9-3d5b-4b8b-9875-6b66634b490c@googlegroups.com> Date: Sun, 17 May 2015 02:59:06 +1000 Subject: Re: Fastest way to remove the first x characters from a very long string From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1431795548 news.xs4all.nl 2872 [2001:888:2000:d::a6]:47939 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90745 On Sun, May 17, 2015 at 2:22 AM, wrote: > # Original Approach > # ----------------- > ss = ss.split("\n") > ss1 = "" > for sdata in ss: > ss1 = ss1 + (sdata[OFFSET:] + "\n") > > > # Chris's Approach > # ---------------- > lines = ss.split("\n") > new_text = "\n".join(line[8:] for line in lines) Ah, yep. This is exactly what str.join() exists for :) Though do make sure the results are the same for each - there are two noteworthy differences between these two. Your version has a customizable OFFSET, where mine is hard-coded; I'm sure you know how to change that part. The subtler one is that "\n".join(...) won't put a \n after the final string - your version ends up adding one more newline. If that's important to you, you'll have to add one explicitly. (I suspect probably not, though; ss.split("\n") won't expect a final newline, so you'll get a blank entry in the list if there is one, and then you'll end up reinstating the newline when that blank gets joined in.) Just remember to check correctness before performance, and you should be safe. ChrisA