Fastest way to remove the first x characters from a very long string

Cameron Simpson cs at zip.com.au
Sat May 16 18:41:14 EDT 2015


On 16May2015 10:35, bruceg113355 at gmail.com <bruceg113355 at gmail.com> wrote:
>On Saturday, May 16, 2015 at 12:59:19 PM UTC-4, Chris Angelico wrote:
>> On Sun, May 17, 2015 at 2:22 AM,  <bruceg113355 at gmail.com> 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)
[...]
>
>Your approach more than meets my requirements.
>Data is formatted correctly and performance is simply amazing.
>OFFSET and \n are small details.

The only comment I'd make at this point is to consider if you really need a 
single string at the end. Keeping it as a list of lines may be more flexible.  
(It will consume more memory.) If you're doing more stuff with the string as 
lines then you'd need to re-split it, and so forth.

Cheers,
Cameron Simpson <cs at zip.com.au>



More information about the Python-list mailing list