slicing the end of a string in a list

John Salerno johnjsal at
Fri Mar 3 04:55:34 CET 2006

Ben Cartwright wrote:

> Actually, it creates a new string instance with the \n character
> removed, then discards it.  The original switches[0] string hasn't
> changed.
> Yes.  You are repeated assigning a new string instance to "line", which
> is then never referenced again.  

Ah, thank you!

> PS - actually, you can accomplish all of the above in a single line of
> code:
>   print [line[:-1] for line in open('C:\\switches.txt')]

Wow, that just replaced 7 lines of code! So *this* is why Python is 
good.   :)

More information about the Python-list mailing list