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

Ian Kelly ian.g.kelly at gmail.com
Sat May 16 12:57:23 EDT 2015


On Sat, May 16, 2015 at 10:22 AM,  <bruceg113355 at gmail.com> wrote:
> # Chris's Approach
> # ----------------
> lines = ss.split("\n")
> new_text = "\n".join(line[8:] for line in lines)

Looks like the approach you have may be fast enough already, but I'd
wager the generator expression could be replaced with:

    map(operator.itemgetter(slice(8, None)), lines)

for a modest speed-up. On the downside, this is less readable.
Substitute itertools.imap for map if using Python 2.x.



More information about the Python-list mailing list