Better way to replace/remove characters in a list of strings.

Marc 'BlackJack' Rintsch bj_666 at
Wed Sep 6 08:54:50 CEST 2006

In <1157497380.382483.127700 at>, George Sakkis

> Chris Brat wrote:
>> Wouldn't this only cause problems with large lists - for once off
>> scripts with small lists it doesn't seem like a big issue to me.
> The extra memory to allocate the new list is usually a minor issue; the
> important one is correctness, if the original list is referenced by
> more than one names.

It's not the allocation of the new list itself that might be an issue but
the content, which will be copied in this case, before the old list and
its content is freed.  If you have 200 MiB worth of strings in the list
and change all 'u's to 'x's with

  large_list = [item.replace('u', 'x') for item in large_list]

another list with 200 MiB strings will be created.

	Marc 'BlackJack' Rintsch

More information about the Python-list mailing list