Reputation: 19164
I have a list :
s = ["sam1", "s'am2", "29"]
I want to replace '
from the whole list.
I need output as
s = ["sam1", "sam2", "30"]
currently I am iterating through the list.
Is there any better way to achieve it?
Upvotes: 2
Views: 137
Reputation: 1293
Sam,
This is the closest way I can think of to do it without iteration. Ultimately it is iterating in some fashion at a very atomic level.
s = ["sam1", "s'am2", "29"]
x = ','.join(s).replace("'","").split(",")
Upvotes: 1
Reputation: 143022
You could try this:
s = [i.replace("'", "") for i in s]
but as pointed out this is still iterating through the list. I can't think of any solution that wouldn't include some sort of iteration (explicit or implicit) of the list at some point.
If you have a lot of data you want to do this to and are concerned about speed, you could evaluate the various approaches by timing them and pick the one that's the fastest, otherwise I would stick with the solution you consider most readable.
Upvotes: 7
Reputation: 5965
You can also use map
and lambda
:
map(lambda a: a.replace("\'",""),s)
Upvotes: 2