how to remove \n in the list
gagsl-py2 at yahoo.com.ar
Wed Apr 16 07:23:35 CEST 2008
En Tue, 15 Apr 2008 20:26:16 -0300, Yves Dorfsman <yves at zioup.com>
> Dan Bishop wrote:
>>>> 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.
More information about the Python-list