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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With