R
reetesh nigam
hi,
l=['5\n', '2\n', '7\n', '3\n', '6\n']
how to remove \n from the given list
l=['5\n', '2\n', '7\n', '3\n', '6\n']
how to remove \n from the given list
hi,
l=['5\n', '2\n', '7\n', '3\n', '6\n']
how to remove \n from the given list
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]
Gabriel said:En Mon, 14 Apr 2008 01:41:55 -0300, reetesh nigaml=['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]
Gabriel said:En Mon, 14 Apr 2008 01:41:55 -0300, reetesh nigaml=['5\n', '2\n', '7\n', '3\n', '6\n']
how to remove \n from the given listl 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 ?
[0, 1, 2, 3, 4]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
# This replaces the elements of the list with new ones.a = [0, 1, 2, 3, 4]
b = a # As before, "a" and "b" refers to the same list
a[:] = [5, 6, 7]
[5, 6, 7]a [5, 6, 7]
b
Dan said:What is the point of the [:] after lines ? How different is it with orlines[:] = [line.rstrip('\n') for line in lines]
without it ?
It causes the result to be stored in the existing list.
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 ?
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 ?
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.