答复: how to remove \n in the list

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Apr 14 04:11:50 EDT 2008


En Mon, 14 Apr 2008 04:08:06 -0300, Penny Y. <pylists at arcor.de> escribió:

>> lines[:] = [line.rstrip('\n') for line in lines]
>
> why not just:
>
> lines = [line.rstrip('\n') for line in lines]
>
>
> what's the difference between lines[:] and lines here? Thanks.

My version (using [:]) replaces the *contents* of the original list, but  
the list itself remains the same object.
Yours (without the [:]) reasigns a new list to the old name; the  
*contents* are the same as the former version, but now the name "lines"  
refers to a different list.
Depending on the context, the difference may be important or not.

-- 
Gabriel Genellina




More information about the Python-list mailing list