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

C

Chris Brat

Hi,

Is there a better way to replace/remove characters (specifically ' and
" characters in my case, but it could be anything) in strings in a
list, than this example to replace 'a' with 'b':



x = ["abbbb","123a","nnnnas"]

for i, v in enumerate(x) :
x = v.replace("a","b")


This works, but I'd like to know peoples opinions.

Thanks
Chris
 
P

Philipp Pagel

Chris Brat said:
Is there a better way to replace/remove characters (specifically ' and
" characters in my case, but it could be anything) in strings in a
list, than this example to replace 'a' with 'b':

x = map(lambda foo: foo.replace('a', 'b'), x)

cu
Philipp
 
B

Bruno Desthuilliers

Chris Brat a écrit :
Thanks, thats exactly what I was looking for - very neat.
Just note that both solutions rebind the name to a newly constructed
list instead of modifying the original list in place. This is usually
the RightThing(tm), but sometimes one wants an in-place modification.
 
C

Chris Brat

Hi

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.

Regards,
Chris
 
G

George Sakkis

Chris said:
Hi

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.

Regards,
Chris


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. Check the following almost identical-looking
cases:
1)
x = ["abbbb","123a","nnnnas"]
y = x
x = [s.replace('a', 'b') for s in x] # rebind to new list
y is x
False

2)
x = ["abbbb","123a","nnnnas"]
y = x
x[:] = [s.replace('a', 'b') for s in x] # in place modification
y is x
True

Neither case is always "the correct"; correctness depends on the
problem at hand, so you should know the difference and decide between
rebinding and mutation accordingly.

George
 
M

Marc 'BlackJack' Rintsch

George Sakkis said:
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.

Ciao,
Marc 'BlackJack' Rintsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top