how to remove \n in the list

Yves Dorfsman yves at zioup.com
Tue Apr 15 19:26:16 EDT 2008


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 ?


Thanks.


Yves.
http://www.SollerS.ca



More information about the Python-list mailing list