how to remove \n in the list

R

reetesh nigam

hi,
l=['5\n', '2\n', '7\n', '3\n', '6\n']

how to remove \n from the given list
 
G

Gabriel Genellina

En Mon, 14 Apr 2008 01:41:55 -0300, reetesh nigam
hi,
l=['5\n', '2\n', '7\n', '3\n', '6\n']

how to remove \n from the given list

l is is very poor name... I'll use lines instead:

lines[:] = [line.rstrip('\n') for line in lines]
 
Y

Yves Dorfsman

Gabriel said:
En Mon, 14 Apr 2008 01:41:55 -0300, reetesh nigam
l=['5\n', '2\n', '7\n', '3\n', '6\n']

how to remove \n from the given list

l is is very poor name... I'll use lines instead:

lines[:] = [line.rstrip('\n') for line in lines]

When I saw the original message, I immediately thought:

k = [x.strip() for x in l]

What is the point of the [:] after lines ? How different is it with or
without it ?

Yves.
http://www.SollerS.ca
 
D

Dan Bishop

Gabriel said:
En Mon, 14 Apr 2008 01:41:55 -0300, reetesh nigam
l=['5\n', '2\n', '7\n', '3\n', '6\n']
how to remove \n from the given list
l is is very poor name... I'll use lines instead:
lines[:] = [line.rstrip('\n') for line in lines]

When I saw the original message, I immediately thought:

k = [x.strip() for x in l]

What is the point of the [:] after lines ? How different is it with or
without it ?

It causes the result to be stored in the existing list.
a = [0, 1, 2, 3, 4]
b = a # "a" and "b" now refer to the same list
a = [5, 6, 7] # "a" now refers to a new list
a [5, 6, 7]
b
[0, 1, 2, 3, 4]
a = [0, 1, 2, 3, 4]
b = a # As before, "a" and "b" refers to the same list
a[:] = [5, 6, 7]
# This replaces the elements of the list with new ones.
# "a" still refers to the same list as "b".
[5, 6, 7]
 
Y

Yves Dorfsman

Dan said:
lines[:] = [line.rstrip('\n') for line in lines]
What is the point of the [:] after lines ? How different is it with or
without it ?

It causes the result to be stored in the existing list.

If we do:
lines = [line.rstrip('\n') for line in lines]

lines is now a new list, the old list as no reference to it, and will be
discarded by the gc, right ? So we're not really saving any space here ?

If we do:
lines[:] = [line.rstrip('\n') for line in lines]

We reuse an existing list, therefore we are saving the time it takes to
create a new list ? So this is a performance issue ?


Thanks.


Yves.
http://www.SollerS.ca
 
A

alex23

If we do:
lines[:] = [line.rstrip('\n') for line in lines]

We reuse an existing list, therefore we are saving the time it takes to
create a new list ? So this is a performance issue ?

I think it's more of a reference issue. You may have other labels
already pointing to 'lines', if you want them to refer to the same,
rstrip'd list, you'd do an in-place update like this.

- alex23
 
G

Gabriel Genellina

Dan said:
lines[:] = [line.rstrip('\n') for line in lines]
What is the point of the [:] after lines ? How different is it with or
without it ?

It causes the result to be stored in the existing list.

If we do:
lines = [line.rstrip('\n') for line in lines]

lines is now a new list, the old list as no reference to it, and will be
discarded by the gc, right ? So we're not really saving any space here ?

If we do:
lines[:] = [line.rstrip('\n') for line in lines]

We reuse an existing list, therefore we are saving the time it takes to
create a new list ? So this is a performance issue ?

No. The new list (the right hand side) is created in either case, so there
is no difference here. And the name `lines` refers to a list with the new
contents in all cases. The difference is on whether *other* references to
the original list see the changes or not.
For an example, see Dan Bishop's message earlier on this thread.
 

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,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top