strip char from list of strings

L

Laurent Luce

I have the following list:

[ 'test\n', test2\n', 'test3\n' ]

I want to remove the '\n' from each string in place, what is the most efficient way to do that ?

Regards,

Laurent
 
P

Piet van Oostrum

Laurent Luce said:
LL> I have the following list:
LL> [ 'test\n', test2\n', 'test3\n' ]
LL> I want to remove the '\n' from each string in place, what is the
LL> most efficient way to do that ?

I suppose you mean you have lists similar to the one given because with
a list of 3 elements efficiency is a non-issue unless you do something
stupid or repeat the operation thousands of times. Even with a list of
1000 elements efficiency isn't very important. In fact you should worry
about efficiency only after there are signs that there might be a
problem.

Secondly, in Python you cannot remove a character from a string in place
if that means modifying the string. Strings are immutable. So let us
suppose you mean that the list should stay in place, i.e if L is the
list that after the operation L is still the same object but L
has become a string that has the value of the old L with the '\n'
removed. Let us also suppose that in the original list every element has
exactly one '\n' at the end.

Then a simple loop should do it. The enumerate comes to mind as you need
both the index and the value of each element:

for i, value in enumerate(L):
L = value[:-1]

But it looks a bit silly to me to refer to the same list element as both
value and L. So maybe the old-fashioned way is more clear:

for i in range(len(L)):
L = L[:-1]

I think the choice is a matter of taste. You can do some timing
experiments yourself to see if there are significant differences, but I
wouldn't expect so.
 
C

Casey Webster

I have the following list:

[ 'test\n', test2\n', 'test3\n' ]

I want to remove the '\n' from each string in place, what is the most efficient way to do that ?

Regards,

Laurent

Do you _really_ need to do this in place? If not, the simplest answer
is probably:
x = ['test\n', test2\n', 'test3\n']
x = [s.rstrip('\n') for s in x]

And if what you really want to do is strip off all trailing whitespace
(tabs, spaces, and newlines), then:

A quick test of 1,000,000 strings of length 27 took less than 0.2
seconds on my PC. Efficiency isn't really much of an issue for most
data sets.
 
L

Laurent Luce

Thanks Casey. I like your solution.

Casey said:
I have the following list:

[ 'test\n', test2\n', 'test3\n' ]

I want to remove the '\n' from each string in place, what is the most efficient way to do that ?

Regards,

Laurent

Do you _really_ need to do this in place? If not, the simplest answer
is probably:
x = ['test\n', test2\n', 'test3\n']
x = [s.rstrip('\n') for s in x]

And if what you really want to do is strip off all trailing whitespace
(tabs, spaces, and newlines), then:
x = [s.rstrip() for s in x]

A quick test of 1,000,000 strings of length 27 took less than 0.2
seconds on my PC. Efficiency isn't really much of an issue for most
data sets.
 
J

Jorgen Grahn

LL> I have the following list:
LL> [ 'test\n', test2\n', 'test3\n' ]
LL> I want to remove the '\n' from each string in place, what is the
LL> most efficient way to do that ?

I suppose you mean you have lists similar to the one given because with
a list of 3 elements efficiency is a non-issue unless you do something
stupid or repeat the operation thousands of times. Even with a list of
1000 elements efficiency isn't very important. In fact you should worry
about efficiency only after there are signs that there might be a
problem.

Secondly, in Python you cannot remove a character from a string in place
if that means modifying the string. Strings are immutable.

So the best way is probably to make sure the '\n's do not end up in
the list in the first place. I suspect that is often more elegant too.

/Jorgen
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top